题解 | #堆栈的使用#
堆栈的使用
https://www.nowcoder.com/practice/e91982a145944ceab6bb9a4a508e0e26
#include<cstdio> #include<stack> using namespace std; int main() { int n ; char x; int y ; stack<int> myStack; while (scanf("%d", &n) != EOF) { for (int i = 0 ; i < n ; ++i) { scanf("%c", &x); if (x == 'P') { scanf("%d", &y); myStack.push(y); } else if (x == 'O') { if (!myStack.empty()) { myStack.pop(); } } else if (x == 'A') { if (myStack.empty()) { printf("E\n"); } else { printf("%d\n", myStack.top()); } } } } }