题解
2024-07-28 14:01:14
发布于:广东
15阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
stack<int> st;
int main() {
    int m;
    cin>>m;
    while(m--){
        string op;
        int x;
        cin>>op;
        if(op=="push"){
            cin>>x;
            st.push(x);
        }
        else if(op=="pop"){
            if(st.empty()) cout<<"pop fail"<<endl;
            else cout<<"pop "<<st.top()<<endl,st.pop(); 
        }
        else if(op=="size") cout<<"size = "<<st.size()<<endl;
        else if(op=="top"){
            if(st.empty()) cout<<"top fail"<<endl;
            else cout<<"top = "<<st.top()<<endl;
        }
        else if(op=="empty"){
            if(st.empty()) cout<<"yes\n";
            else cout<<"no\n";
        }
    }
    return 0;
}
这里空空如也

有帮助,赞一个