题解 | #【模板】栈#

【模板】栈

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

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {
    int n;
    cin >> n;
    string temp;
    int num = 0;
    int x;
    int counts[100001];
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == "push") {
            cin >> x;
            counts[num] = x;
            num++;
        } else {
            if (num > 0) {
                if (temp == "pop") {
                    cout << counts[num - 1] << endl;
                    num--;
                } else {
                    cout << counts[num - 1] << endl;
                }
            } else {
                cout << "error" << endl;
            }
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

05-09 12:23
已编辑
华南理工大学 Java
野猪不是猪🐗:给他装的,双九+有实习的能看的上这种厂我直接吃⑨✌们拿它练练面试愣是给他整出幻觉了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务