题解 | 【模板】栈

【模板】栈

https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf

#include<bits/stdc++.h>
using namespace std;
int a[3000000];
int main(){
   int n;cin>>n;
   int top=0,x;string s;
for(int i=0;i<n;i++){
cin>>s;
if(s=="push")cin>>x,a[top++]=x;//输入
else if(top==0)cout<<"error"<<endl;//判断栈空
else if(s=="pop")cout<<a[--top]<<endl;//输出栈顶并弹出
else cout<<a[top-1]<<endl;//输出栈顶
}
    return 0;
}

全部评论

相关推荐

11-03 18:50
门头沟学院 Java
迷茫的大四🐶:问就是马上到,一周五天,6个月以上,全国可飞
点赞 评论 收藏
分享
11-19 18:44
已编辑
成都理工大学 Java
程序员花海:我面试过100+校招生,大厂后端面试不看ACM,竞赛经历含金量低于你有几份大厂实习 这个简历整体来看不错 可以海投
如何写一份好简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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