stack操作 + 分支语句
2023-08-02 22:08:44
发布于:上海
57阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    stack<int> st;
    for(int i=1;i<=n;i++){
        string k;
        cin>>k;
        if(k=="empty" and st.empty()){
            cout<<"yes";
            cout<<endl;
            continue;
        }
        if(k=="empty" and !st.empty()){
            cout<<"no";
            cout<<endl;
            continue;
        }
        if(k=="push"){
            int num;
            cin>>num;
            st.push(num);
            continue;
        }
        if(k=="pop"){
            if(st.empty()) cout<<"pop fail";
            else{
                cout<<k<<" "<<st.top();
                st.pop();
            }
            cout<<endl;
            continue;
        }
        if(k=="top"){
            if(st.empty()) cout<<"top fail";
            else{
                cout<<k<<" = "<<st.top();
            }
            cout<<endl;
            continue;
        }
        if(k=="size"){
            cout<<"size = "<<st.size();
            cout<<endl;
            continue;
        }
    }
    return 0;
}
这里空空如也



有帮助,赞一个