题解 100% AC
2025-06-14 11:28:58
发布于:浙江
1阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
int s[110];
int TOP,n;
void push(int x){
s[++TOP]=x;
}
void pop(){
--TOP;
}
int top(){
return s[TOP];
}
bool empty(){
return TOP==0;
}
int size(){
return TOP;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
string x;
cin>>x;
if(x=="push"){
int a;cin>>a;
push(a);
}else if(x=="pop"){
if(empty()) cout<<"pop fail"<<endl;
else{
cout<<"pop "<<top()<<endl;
pop();
}
}else if(x=="top"){
if(empty()) cout<<"top fail"<<endl;
else cout<<"top = "<<top()<<endl;
}else if(x=="size") cout<<"size = "<<size()<<endl;
else if(x=="empty"){
if(empty()) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
}
return 0;
}
这里空空如也
有帮助,赞一个