题解 | 堆栈的使用

堆栈的使用

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

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

int main() {
    int n;
    while (cin >> n) {
        stack<int> s;
        while (n--) {
            char ch;
            cin >> ch;
            switch (ch) {
                case 'A':
                    if (s.empty()) cout << 'E' << endl;
                    else cout << s.top() << endl;
                    break;
                case 'P':
                    int i;
                    cin >> i;
                    s.push(i);
                    break;
                case 'O':
                    if (s.empty());
                    else s.pop();
                    break;
            }
        }
    }
    return 0;
}

全部评论

相关推荐

牛至超人:我将凌晨两点给你打电话
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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