题解 | #【模板】栈#

【模板】栈

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

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int st[N];
int n, x, top = -1;
string s;
int main()
{
    cin>>n;
    while (n--) {
        cin>>s;
        if (s == "push") {
            cin>>x;
            st[++top] = x;
        }else if (s == "top") {
            if (top == -1) {
                cout<<"error"<<endl;
            } else {
                cout<<st[top]<<endl;
            }
        }else if (s == "pop") {
            if (top == -1) {
                cout<<"error"<<endl;
            } else {
                cout<<st[top]<<endl;
                --top;
            }
        }
    }
    
    return 0;
}
全部评论

相关推荐

迷茫的大四🐶:自信一点,我认为你可以拿到50k,低于50k完全配不上你的能力,兄弟,不要被他们骗了,你可以的
点赞 评论 收藏
分享
待现的未见之事:起码第一句要把自己的优势说出来吧。比如什么xx本27届学生,随时到岗....
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务