CE求救

B3614 【模板】栈

kkksc24 @ 2024-12-02 20:52:15

#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
int main(){
    stack<ull> st;
    ull b;
    int T,n;
    string tmp;
    cin>>T;
    for(int i=1;i<=T;i++){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>tmp;
            if(tmp=="push"){
                cin>>b;
                st.push(b);
            }
            if(tmp=="pop"){
                if(st.empty) cout<<"Empty"<<endl;
                else st.pop;
            }
            if(tmp=="query"){
                if(st.empty) cout<<"Anguei!"<<endl;
                else cout<<st.top()<<endl;
            }
            if(tmp=="size") cout<<st.size()<<endl;
        }
    }
    return 0;
}

by 陈嘉逸2012 @ 2024-12-02 20:55:57

@zhongjohn emptypop后面要加括号。


by DioxygenDifluoride @ 2024-12-02 20:59:17

楼⬆️正解


by XDuanZai @ 2024-12-02 21:12:58

empty和pop后面要加个括号,不然会编译错误。@zhongjohn


by kkksc24 @ 2024-12-03 20:13:42

改过66pts求救

#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
int main(){
    stack<ull> st;
    ull b;
    int T,n;
    string tmp;
    cin>>T;
    for(int i=1;i<=T;i++){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>tmp;
            if(tmp=="push"){
                cin>>b;
                st.push(b);
            }
            if(tmp=="pop"){
                if(st.empty()) cout<<"Empty"<<endl;
                else st.pop();
            }
            if(tmp=="query"){
                if(st.empty()) cout<<"Anguei!"<<endl;
                else cout<<st.top()<<endl;
            }
            if(tmp=="size") cout<<st.size()<<endl;
        }
    }
    return 0;
}

by XDuanZai @ 2024-12-03 21:09:39

@zhongjohn 把st.empty()改为st.size()==0 求互关


by XDuanZai @ 2024-12-03 21:11:36

@zhongjohn 判断栈是否为空尽量不要用empty函数。(求互关)


|