logo AlgoBeat OnlineJudge
登录 注册

题解

作者: Steven1356  ·  发布于 2026-09-26 20:37:23  ·  最后修改于 2026-09-26 21:37:52
已通过
审核员:Lemon_zqp 弱弱 · 2026-09-26 21:37:52

考察对于栈的各种操作,会用 STL 即可。

  • push 进栈(如在 x 中添加 为 x.push(1))
  • pop 出栈(如 x.pop())
  • top 获取最上面的元素(如 x.top())
  • size 大小(如 x.size())
#include<bits/stdc++.h>
using namespace std;
stack<int> x;
signed main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        string s;
        cin>>s;
        if(s=="push"){
            int m;
            cin>>m;
            x.push(m);
        }else if(s=="pop"){
            if(x.empty())cout<<"Empty"<<endl;
            else x.pop();
        }
        else if(s=="query"){
            if(x.empty())cout<<"Anguei!"<<endl;
            else cout<<x.top()<<endl;
        }else cout<<x.size()<<endl;
    }
    return 0;
}

暂无评论

登录 后即可评论。