题解 | 【模板】栈
【模板】栈
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;
}

汤臣倍健公司氛围 406人发布
