队列练习1,2,3

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 int q[300000],n,head,tail,a,b;
 4 int main(){
 5     cin>>n;
 6     while(n--){
 7         cin>>a;
 8         if(a==1){
 9             cin>>b;
10             q[head++]=b;
11         }
12         if(a==2) tail++;
13     }
14     if(head-tail) cout<<q[tail];
15     else cout<<"impossible!";
16     cout<<endl;
17     return 0;
18 }

STL

#include<queue>
#include<iostream>
using namespace std;
queue<int> q;
int a,b,n;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a;
		if(a==1){
			cin>>b;
			q.push(b);
		}
		if(a==2) q.pop();
	}
	if(q.empty()) cout<<"impossible!";
	else cout<<q.front();
	return 0;
}

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 int q[300000],n,head,tail,a,b;
 4 int main(){
 5     cin>>n;
 6     while(n--){
 7         cin>>a;
 8         if(a==1){
 9             cin>>b;
10             q[head++]=b;
11         }
12         if(a==2){
13             if(tail==head) {cout<<"impossible!"<<endl;return 0;}
14             tail++;
15         }
16     }
17     if(head>tail) cout<<q[tail];
18     else cout<<"impossible!";
19     cout<<endl;
20     return 0;
21 }

STL

 1 #include<queue>
 2 #include<iostream>
 3 using namespace std;
 4 queue<int> q;
 5 int a,b,n;
 6 int main(){
 7     cin>>n;
 8     for(int i=1;i<=n;i++){
 9         cin>>a;
10         if(a==1){
11             cin>>b;
12             q.push(b);
13         }
14         if(a==2){
15             if(q.empty()){cout<<"impossible!";return 0;}
16         q.pop();
17     }
18     }
19     if(!q.empty()) cout<<q.front();
20     else cout<<"impossible!";
21     return 0;
22 }

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 int q[300000],n,head,tail,a,b;
 4 int main(){
 5     cin>>n;
 6     while(n--){
 7         cin>>a;
 8         if(a==1){
 9             cin>>b;
10             q[head++]=b;
11         }
12         if(a==2) tail++;
13         if(a==3) cout<<q[tail]<<endl;
14     }
15     return 0;
16 }

STL

 1 #include<queue>
 2 #include<iostream>
 3 using namespace std;
 4 queue<int> q;
 5 int n,a,b;
 6 int main(){
 7     cin>>n;
 8     while(n--){
 9         cin>>a;
10         if(a==1){
11             cin>>b;
12             q.push(b);
13         }
14         if(a==2) q.pop();
15         if(a==3) cout<<q.front()<<endl;
16     }
17     return 0;
18 }

题目来源:CODE[VS]

转载于:https://www.cnblogs.com/J-william/p/6194608.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值