33风求条,码风清晰良好

B3614 【模板】栈

shufeng2007 @ 2024-10-24 08:57:57

#include<bits/stdc++.h>
using namespace std;
int t,n;
int main(){
    cin>>t;
    while(t--){
        queue<int> q;
        int n;
        cin>>n;
        while(n--){
            string s;
            cin>>s;
            if(s=="push"){
                int a;
                cin>>a;
                q.push(a);
            }
            else if(s=="pop"){
                if(q.size()==0) cout<<"Empty"<<endl;
                else q.pop();
            }
            else if(s=="query"){
                if(q.size()==0) cout<<"Anguei!"<<endl;
                else cout<<q.front()<<endl;
            }
            else cout<<q.size()<<endl;
        }
    }

    return 0;
} 

by lichongkai @ 2024-10-24 09:02:02

你确定你用的是栈


by zhizhizhiwang @ 2024-10-24 09:05:36

queue是队列捏 stack才是栈


by zhaohanwen @ 2024-10-24 09:08:20

已经AC。

#include<bits/stdc++.h>
using namespace std;
int t;
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--){
        stack<unsigned long long> q;
        int n;
        cin>>n;
        while(n--){
            string s;
            cin>>s;
            if(s=="push"){
                unsigned long long a;
                cin>>a;
                q.push(a);
            }
            else if(s=="pop"){
                if(q.size()==0) cout<<"Empty"<<'\n';
                else q.pop();
            }
            else if(s=="query"){
                if(q.size()==0) cout<<"Anguei!"<<'\n';
                else cout<<q.top()<<endl;
            }
            else cout<<q.size()<<endl;
        }
    }

    return 0;
} 

@shufeng2007


by Scez @ 2024-10-24 09:08:32

@shufeng2007 把队列换成栈并且改一下输出栈顶就好了捏(判空可以直接用empty() )


by zhaohanwen @ 2024-10-24 09:09:00

@shufeng2007 注意数据范围开unsigned long long,栈是 stack


by zhaohanwen @ 2024-10-24 09:09:40

@shufeng2007 另外关流来加快读入


|