语言:C++11

方法:库函数stack的基本操作

#include "iostream"
#include "stack"

using namespace std;

int main() {
    // I/O 加速
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    while (cin >> n) {                                          // 用于多组数据输入
        stack<int> Stack;                                       // 栈
        for (int i = 0; i < n; ++i) {                           // 处理接下来的n组数据
            char input;                                         // 用于接收输入的 P(压栈) O(出栈) A(问询)
            int inputNum;
            cin >> input;
            if (input == 'P') {                                 // 情况 P(压栈)
                cin >> inputNum;
                Stack.push(inputNum);
            } else if (input == 'O' && !Stack.empty()) {        // 情况 O(出栈)
//                cout << Stack.top() << endl; // 不需要输出
                Stack.pop();
            } else if (input == 'A') {
                if (!Stack.empty()) cout << Stack.top() << endl; // 情况 A(问询)
                else cout << "E" << endl;
            }
        }
    }
    return 0;
}