题解 | 【模板】栈
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
int n,s[N],topIdx;
void push(int x){
s[topIdx++] = x;
}
void top(){
if(!topIdx) cout<<"error\n";
else cout<<s[topIdx-1]<<"\n";
}
void pop(){
if(!topIdx) cout<<"error\n";
else cout<<s[--topIdx]<<"\n";
}
int main(){
cin>>n;
while(n--){
string op;
cin>>op;
if("push"==op){
int x;cin>>x;
push(x);
}
else if("pop"==op){
pop();
}
else{
top();
}
}
return 0;
}
#牛客春招刷题训练营#https://www.nowcoder.com/discuss/727521113110073344
查看17道真题和解析