题解 | #【模板】栈#

【模板】栈

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")

全部评论

相关推荐

牛客吹哨人:哨哥晚点统一更新到黑名单:能救一个是一个!26届毁意向毁约裁员黑名单https://www.nowcoder.com/discuss/1525833
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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