博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
纸牌游戏(队列)
阅读量:6423 次
发布时间:2019-06-23

本文共 2762 字,大约阅读时间需要 9 分钟。

纸牌游戏解题报告

在写题目之前,还是要讲一下队列。

队列:“先进先出”,又称公平队列。注意:队列不需要定义大小。

头文件:<queue>   定义/声明方式:queue<int> s;

push():入队、     pop():出队  

front():取队首元素,但不删除,返回queue内的第一个元素

back():返回queue内的最后一个元素

 

题目:

Description

Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a "war"-like card game.

The rules are following. On each turn a fight happens. Each of them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins this fight and takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty, he loses and the other one wins.

You have to calculate how many fights will happen and who will win the game, or state that game won't end.

Input

First line contains a single integer n (2 ≤ n ≤ 10), the number of cards.

Second line contains integer k1 (1 ≤ k1 ≤ n - 1), the number of the first soldier's cards. Then follow k1 integers that are the values on the first soldier's cards, from top to bottom of his stack.

Third line contains integer k2 (k1 + k2 = n), the number of the second soldier's cards. Then follow k2 integers that are the values on the second soldier's cards, from top to bottom of his stack.

All card values are different.

Output

If somebody wins in this game, print 2 integers where the first one stands for the number of fights before end of game and the second one is 1 or 2 showing which player has won.

If the game won't end and will continue forever output  - 1.

Sample Input

 
Input
4 2 1 3 2 4 2
Output
6 2
Input
3 1 2 2 1 3
Output
-1

Sample Output

 

Hint

First sample:

Second sample:

题目大意:

两人打牌,牌总数不超过10张,牌的面值也不同。按顺序出牌,然后比较大小,大的那方赢了,得到对方的牌,并把这两张牌都放到队尾,一直这样做下去,知道一方手中没有牌为止,没有牌的那方输掉比赛。如果比赛不能结束,输出-1;如果比赛结束,输出结束比赛前所玩游戏的次数和赢的人。

分析:

很容易看出来,这是一道关于队列的问题。建立两个队列并将牌放入各自队列中,每次取出队列的第一个元素并比较大小,赢的人把两个牌全都放入队尾,用ans记录,比一次ans++。没有结束ans=-1。

代码:

1 #include
2 #include
3 using namespace std; 4 int main() 5 { 6 queue
q1,q2; 7 int n,k1,k2,a; 8 while(scanf("%d",&n)!=EOF) 9 { 10 scanf("%d",&k1); 11 for(int i=0;i
v) 32 { 33 q1.push(v); 34 q1.push(u); 35 } 36 else 37 { 38 q2.push(u); 39 q2.push(v); 40 } 41 ans++; 42 if(ans>1e6) 43 { 44 ans=-1; 45 break; 46 } 47 48 } 49 printf("%d",ans); 50 if(ans!=-1) 51 printf(" %d\n",q1.empty()?2:1); 52 } 53 return 0; 54 }

 

心得:

栈和队列有很多相似的地方,应该比较学习。看了别人的代码,发现自己还有很多不足,要努力改正,好好学习。

 

转载于:https://www.cnblogs.com/ttmj865/p/4677715.html

你可能感兴趣的文章
技术助力第三次革命
查看>>
《HTML与CSS入门经典(第8版)》——2.6 总结
查看>>
新手指南:在 Ubuntu 和 Fedora 上安装软件包
查看>>
在 CentOS7.0 上搭建 Chroot 的 Bind DNS 服务器
查看>>
《Python高性能编程》——2.2 Julia集合的介绍
查看>>
大型网站的 HTTPS 实践(二):HTTPS 对性能的影响
查看>>
《Swift 权威指南》——第6章,第6.10节嵌套函数
查看>>
《自己动手做交互系统》——1.3 本章小结
查看>>
Mobile devices bundled with malware?
查看>>
《Node应用程序构建——使用MongoDB和Backbone》一导读
查看>>
《JavaScript面向对象精要》——1.5 访问属性
查看>>
《Python数据可视化编程实战》—— 第 1 章 准备工作环境
查看>>
Android应用性能优化最佳实践.1.1 Android Studio的优势
查看>>
《设计模式解析(第2版•修订版)》—第2章 2.2节什么是UML
查看>>
【健康医疗】4步完成数据分析报表,让医疗数据转化为生产力
查看>>
【直播】APP全量混淆和瘦身技术揭秘
查看>>
10个大坑,当你产品上架AppStore会遇到
查看>>
【shell 脚本】两种登录方式
查看>>
echars 动态加载数据
查看>>
学习编程的方法
查看>>