题解 | 堆栈的使用

#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,所以会出一个越界错误,只要把这点给干掉就可以轻松解决了

全部评论

相关推荐

球Offer上岸👑:可能是大环境太差了 太卷了 学历也很重要 hc也不是很多 所以很难
点赞 评论 收藏
分享
04-11 21:31
四川大学 Java
野猪不是猪🐗:(ja)va学弟这招太狠了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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