AC 代码
#include <bits/stdc++.h>
using namespace std;
/*====================*/
typedef long long ll;
/*====================*/
#define endl '\n'
/*====================*/
const int N=1e5+10;
/*====================*/
int n,stk[N],top;
/*====================*/
void Solve(void)
{
cin>>n;
stk[0]=0;
for(int i=1;i<=n;i++)
{
int op;cin>>op;
if(op==0)
{
int x;cin>>x;
top++;
stk[top]=max(stk[top-1],x);
}
else if(op==1)
{
if(top!=0) top--;
}
else if(op==2)
{
cout<<stk[top]<<endl;
}
}
}
/*====================*/
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
int T=1;//cin>>T;
while(T--) Solve();
return 0;
}
如果把
if(op==0)
{
int x;cin>>x;
top++;
stk[top]=max(stk[top-1],x);
}
改成
if(op==0)
{
int x;cin>>x;
stk[++top]=max(stk[top-1],x);
}
就会WA掉