3185 队列练习 1 3186 队列练习 2

3185 队列练习 1

 

时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
 
 
 
题目描述 Description

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

输入描述 Input Description

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

输出描述 Output Description

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

样例输入 Sample Input

3
1 2
1 9
2

样例输出 Sample Output

9

数据范围及提示 Data Size & Hint

对于100%的数据 N≤1000 元素均为正整数且小于等于100

 1 #include<iostream>
 2 using namespace std;
 3 #include<cstdio>
 4 int d[10000];
 5 int head=1,tail=1;
 6 int main()
 7 {
 8     int n,x,y;
 9     cin>>n;
10     for(int i=1;i<=n;++i)
11     {
12         scanf("%d",&x);
13         if(x==1){
14             scanf("%d",&y); 
15             d[tail++]=y;
16         }
17         if(x==2)
18         {
19             head++;
20         }
21     }
22     if(head==tail)cout<<"impossible!";
23     else cout<<d[head];
24     return 0;
25 }

3186 队列练习 2

 

时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
 
 
 
题目描述 Description

(此题与队列练习1相比改了2处:1加强了数据 2不保证队空时不会出队)
给定一个队列(初始为空),只有两种操作入队和出队,现给出这些操作请
输出最终的队头元素。 操作解释:1表示入队,2表示出队

输入描述 Input Description

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

输出描述 Output Description

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

样例输入 Sample Input

3
1 2
2
2

样例输出 Sample Output

impossible!

数据范围及提示 Data Size & Hint

对于100%的数据  N≤100000 元素均为正整数且小于等于10^8

 1 #include<iostream>
 2 using namespace std;
 3 #include<cstdio>
 4 int d[100000];
 5 int head=1,tail=1;
 6 int main()
 7 {
 8     int n,x,y,b=1;
 9     cin>>n;
10     for(int i=1;i<=n;++i)
11     {
12         scanf("%d",&x);
13         if(x==1){
14             scanf("%d",&y); 
15             d[tail++]=y;
16         }
17         if(x==2)
18         {
19             head++;if(head>tail)b=0;
20         }
21     }
22     if(head==tail||b==0)cout<<"impossible!";
23     else cout<<d[head];
24     return 0;
25 }

3187 队列练习 3

 

时间限制: 1 s
空间限制: 128000 KB
题目等级 : 钻石 Diamond
 
 
 
题目描述 Description

比起第一题,本题加了另外一个操作,访问队头元素(编号3,保证访问队头元素时或出队时队不为空),现在给出这N此操作,输出结果。

输入描述 Input Description

N
N次操作(1入队 2出队 3访问队头)

输出描述 Output Description

K行(K为输入中询问的个数)每次的结果

样例输入 Sample Input

6
1 7
3
2
1 9
1 7
3

样例输出 Sample Output

7
9

数据范围及提示 Data Size & Hint

对于50%的数据 N≤1000 入队元素≤200
对于100%的数据 N≤100000入队元素均为正整数且小于等于10^4

 1 #include<iostream>
 2 using namespace std;
 3 #include<cstdio>
 4 int d[100000];
 5 int head=1,tail=1;
 6 int main()
 7 {
 8     int n,x,y,b=1;
 9     cin>>n;
10     for(int i=1;i<=n;++i)
11     {
12         scanf("%d",&x);
13         if(x==1){
14             scanf("%d",&y); 
15             d[tail++]=y;
16         }
17         if(x==2)
18         {
19             head++;
20         }
21         if(x==3)
22         {
23             cout<<d[head]<<endl;
24         }
25     }
26     return 0;
27 }
原文地址:https://www.cnblogs.com/mjtcn/p/6665603.html