题解
2023-09-09 11:06:16
发布于:江苏
11阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
stack <int> stk;
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        string s;
        cin>>s;
        int x;      
        if(s=="push"){
            cin>>x;
            stk.push(x);
        }else if(s=="pop"){
            if(stk.empty()){
                cout<<"pop fail"<<endl;
            }else{
                cout<<"pop "<<stk.top()<<endl;
                stk.pop();
            }
        }else if(s=="top"){
            if(!stk.empty()){
                cout<<"top = "<<stk.top()<<endl;
            }else{
                cout<<"top fail"<<endl;
            }
        }else if(s=="size"){
            cout<<"size = "<<stk.size()<<endl;
        }else if(s=="empty"){
            if(stk.empty()){
                cout<<"yes"<<endl;
            }else{
                cout<<"no"<<endl;
            }
        }
    }
    return 0;
}
全部评论 1
  - 2025-07-27 来自 浙江 0









有帮助,赞一个