88889999
2025-07-27 10:41:46
发布于:广东
#include<bits/stdc++.h>
#include <stack>
using namespace std;
int main() {
int n, x;
cin >> n;
string cs;
stack <int> st;
for(int i = 1; i <= n; ++i) {
cin >> cs;
if(cs == "push") {
cin >> x;
st.push(x);
} else if(cs=="pop") {
if(!st.empty()) {
cout<<"pop"<<" "<<st.top()<<endl;
st.pop();
} else {
cout<<"pop fail"<<endl;
}
} else if(cs=="top") {
if(!st.empty()) {
cout<<"top = "<<st.top()<<endl;
} else {
cout<<"top fail"<<endl;
}
} else if(cs=="size") {
cout<<"size = "<<st.size()<<endl;
} else if(cs=="empty") {
if(!st.empty()){
cout<<"no"<<endl;
}else{
cout<<"yes"<<endl;
}
}
}
return 0;
}
这里空空如也
有帮助,赞一个