题解 | #堆栈的使用#

堆栈的使用

https://www.nowcoder.com/practice/e91982a145944ceab6bb9a4a508e0e26

#include <iostream>
#include <stack>
using namespace std;

stack<int> stk;
int main() {
    int n;
    while (cin >> n) {
        char op;
        int num;
        while(n --){
            cin >> op;
            if(op == 'P'){
                cin >> num;
                stk.push(num);
            }
            else if (op == 'A') {
                if(!stk.empty())
                    cout << stk.top() << endl;
                else
                    cout << 'E' << endl;
            }
            else if (op == 'O'){
                if(!stk.empty())
                    stk.pop();
            }
        }
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

龙珠传说:nb,公务员解约不需要支付违约金吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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