题解 | #【模板】栈#

【模板】栈

https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf

#include <bits/stdc++.h>
using namespace std;

int main() {

    stack<int> s;
    int n;
    cin>>n;
    while(n--)
    {
        string i;
        cin>>i;
        if(i=="push")
        {
            int t;
            cin>>t;
            s.push(t);
        }
        else if(i=="top")
        {
            if(s.size()!=0)
                cout<<s.top()<<endl;
            else 
                cout<<"error\n";
        }
        else if(i=="pop")
        {
            if(s.size()!=0)
           {
            int o=s.top();
            cout<<o<<endl;
            s.pop();
            }
            else {
                cout<<"error\n";
            }
        }
    }
}
// 64 位输出请用 printf("%lld")

菜鸟代码,高手勿喷

全部评论

相关推荐

头像
04-29 10:53
已编辑
东北大学 自动化类
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务