题解 | 堆栈的使用
#include <bits/stdc++.h> using namespace std; int main(){ int n; while(cin>>n){ stack<int>st; while(n--){ char a; cin>>a; if(a=='A'){ if(st.empty())cout<<'E'<<endl; else cout<<st.top()<<endl; } if(a=='P'){ int x; cin>>x; st.push(x); } if(a=='O'){ if(!st.empty())st.pop(); } } } }
这道题能卡这么多人的原因就一个,它存在错误操作,也就是空栈的时候还会pop,所以会出一个越界错误,只要把这点给干掉就可以轻松解决了