题解
2025-10-12 17:00:54
发布于:上海
0阅读
0回复
0点赞
#include <iostream>
#include <stack>
using namespace std;
stack <int> q;
int main(){
int n;
cin >> n;
for (int i=1; i<=n; i++){
string s;
cin >> s;
if (s == "empty"){
cout << (q.empty() ? "yes" : "no") << endl;
}else if (s == "push"){
int k;
cin >> k;
q.push(k);
}else if (s == "pop"){
if (q.empty()){
cout << "pop fail" << endl;
}else{
cout << "pop " << q.top() << endl;
q.pop();
}
}else if (s == "size"){
cout << "size = " << q.size() << endl;
}else if (s == "top"){
if (q.empty()){
cout << "top fail" << endl;
}else{
cout << "top = " << q.top() << endl;
}
}
}
return 0;
}
这里空空如也







有帮助,赞一个