题解 | #【模板】栈#
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
#include <iostream>
using namespace std;
const int N = 100010;
int main()
{
int n;
int stk[N],tt = -1;
cin >> n;
while(n--)
{
string op;
cin >> op;
if(op == "push")
{
int x;
cin >> x;
stk[ ++ tt] = x;
}
else if(op == "pop")
{
if(tt == -1)
cout << "error" << endl;
else
{
cout << stk[tt] << endl;
tt --;
}
}
else if(op == "top")
{
if(tt == -1)
cout << "error" << endl;
else
cout << stk[tt] << endl;
}
}
return 0;
}
查看20道真题和解析