标签:
(此题与队列练习1相比改了2处:1加强了数据 2不保证队空时不会出队)
给定一个队列(初始为空),只有两种操作入队和出队,现给出这些操作请
输出最终的队头元素。 操作解释:1表示入队,2表示出队
N(操作个数)
N个操作(如果是入队则后面还会有一个入队元素)
具体见样例(输入保证队空时不会出队)
最终队头元素,若最终队空,或队空时有出队操作,输出”impossible!”(不含引号)
3
1 2
2
2
impossible!
对于100%的数据 N≤100000 元素均为正整数且小于等于10^8
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int a[100010],n;
int main()
{
int x,y,head=0,tail=0;
cin>>n;
for(int i=1;i<=n;i++)
{
scanf("%d",&y);
if(y==1)
{
scanf("%d",&x);
a[tail]=x;
tail++;
}
if(y==2)
{
head++;
}
if(head>=tail) {
printf("impossible!\n");
return 0;
}
}
if(head>=tail) {
printf("impossible!\n");
}
else
printf("%d\n",a[head]);
return 0;
}(A了90分)
标签:
原文地址:http://www.cnblogs.com/zhishenduchuang/p/5750520.html