题解 | #堆栈的使用#
堆栈的使用
https://www.nowcoder.com/practice/e91982a145944ceab6bb9a4a508e0e26
#include<iostream>
#include<stack>
using namespace std;
int main(){
int n;
while(cin>>n){
char op;
stack<int> stk;
while(n--){
cin>>op;
if(op == 'P'){//压栈
int m;
cin>>m;
stk.push(m);
}else if(op == 'O'){//弹栈
if(!stk.empty()) stk.pop();
}else if(op == 'A'){//输出栈顶
if(stk.empty()){
cout<<'E'<<endl;
}else{
cout<<stk.top()<<endl;
}
}
}
}
return 0;
}
王道考研机试 文章被收录于专栏
包含考研机试打卡表题目
查看17道真题和解析