题解
2025-06-14 11:26:44
发布于:浙江
6阅读
0回复
0点赞
#include<iostream>
using namespace std;
int s[110],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=0;i<n;i++){
        string op;
        cin>>op;
        if(op=="push"){
            int x;
            cin>>x;
            push(x);
        }else if(op=="pop"){
            if(!empty()){
                cout<<"pop "<<Top()<<'\n';
                pop();
            }else{
                cout<<"pop fail\n";
            }
        }else if(op=="top"){
            if(!empty()){
                cout<<"top = "<<Top()<<'\n';
            }else{
                cout<<"top fail\n";
            }
        }else if(op=="size"){
            cout<<"size = "<<size()<<'\n';
        }else if(op=="empty"){
            if(empty()){
                cout<<"yes\n";
            }else{
                cout<<"no\n";
            }
        }
    }
    return 0;
}
这里空空如也







有帮助,赞一个