STL 队列练习

这篇博客通过codevs上的三道题目介绍了如何使用STL队列进行入队、出队操作,并处理特殊情况,如队空时的操作。通过实例展示了队列操作的过程及其结果,帮助读者理解STL队列的运用。
摘要由CSDN通过智能技术生成

codevs 3185
题目描述 Description

给定一个队列(初始为空),只有两种操作入队和出队,现给出这些操作请输出最终的队头元素。 操作解释:1表示入队,2表示出队
输入描述 Input Description

N(操作个数)
N个操作(如果是入队则后面还会有一个入队元素)
具体见样例(输入保证队空时不会出队)
输出描述 Output Description

最终队头元素,若最终队空,输出”impossible!”(不含引号)

样例输入 Sample Input

3
1 2
1 9
2
样例输出 Sample Output

9

#include<iostream>
#include<queue>
using namespace std;
int a[1001];
int head=0,tail=0;
int y;
queue<int>q;
int main()
{
    int t,cz,p;
    cin>>t;
    while(t--)
    {
        cin>>cz;
        if(cz==1)
        {
            cin>>p;
            q.push(p);
        }
        if(cz==2)
        {
            q.pop();
        }
    }
    if(q.empty()) cout<<"impossible!"<<endl;
    else cout<<q.front()<<endl;
    return 0;
}

手打版

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[1001]={
  0},n,t,m,head,tail;
bool empty( );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值