logo AlgoBeat OnlineJudge
登录 注册

队列题解

作者: Steven1356  ·  发布于 2026-08-17 21:39:12  ·  最后修改于 2026-08-18 13:55:02
已通过
审核员:Carey_chen HCl · 2026-08-18 13:55:02

首先,各位应该都会队列的基本操作。

然后我们挨个实现即可,注意判断队列是否为空。

#include<bits/stdc++.h>
using namespace std;
signed main(){
    int n;
    cin>>n;
    queue<int> q;
    for(int i=0;i<n;i++){
        int s;
        cin>>s;
        if(s==1){
            int x;
            cin>>x;
            q.push(x);
        }else if(s==2){
            if(q.empty())cout<<"ERR_CANNOT_POP"<<endl;
            else q.pop();
        }else if(s==3){
            if(q.empty())cout<<"ERR_CANNOT_QUERY"<<endl;
            else cout<<q.front()<<endl;
        }else{
            cout<<q.size()<<endl;
        }
    }
    return 0;
}

暂无评论

登录 后即可评论。