题解 | 【模板】栈

【模板】栈

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

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

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

#define endl '\n'
#define nl cout << '\n'
#define den(a) cout << #a << " = " << a << '\n';
#define deg(a) cout << #a << " = " << a << "  ";
#define yes() cout << "Yes\n"
#define no() cout << "No\n"

const int N = 2e5 + 10, Mod = 998244353;


void solve() {
    int n;
    cin >> n;
    stack<int> q;
    while (n --) {
        string op;
        cin >> op;
        if(op == "push") {
            int x;
            cin >> x;
            q.push(x);
        } else if(op == "pop") {
            if(q.size()) {
                cout << q.top() << endl;
                q.pop();
            } else cout << "error" << endl;
        } else {
            if(q.size()) {
                cout << q.top() << endl;
            } else cout << "error" << endl;
        }
    }
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    //cin >> t;
    while (t--)solve();
    return 0;
}

直接用C++自带的stack即可

全部评论

相关推荐

前两题写得快,最后一题也只会个暴力,结束笔试
洛水为天依:第三题卡在了71%,实在看不出来错哪了直接交卷了
投递米哈游等公司10个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-28 00:10
已编辑
码农索隆:这哥们库库在我帖子下评论
点赞 评论 收藏
分享
还在公海池里。。。&nbsp;能不能给孩子一次面试机会。。。&nbsp;不知道在海里游多久能上岸
我只是一个小白菜:人才库就人才库,还搞个公海
投递京东等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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