打牌~~~
2015-07-24 22:59
387 查看
题意: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.InputFirst 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.OutputIf 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.Input
4 2 1 3 2 4 2 Output 6 2
思路: 用队列模拟两个玩家打牌,比较两个玩家最顶上的大小,一直到有玩家没牌了。还有一种就是平手,直接设置为循环1000次之后还没出结果就是循环。 源代码:
#include<iostream> #include<queue> #include<cstring> using namespace std; queue<int>p1, p2; int main() { int t; cin >> t; int count = 0; int a1, a2; int i = 0; int n; int m; cin >> n; for (i = 0; i<n; i++) { cin >> a1; p1.push(a1); } cin >> m; for (i = 0; i<m; i++) { cin >> a2; p2.push(a2); } while (1) { if (p1.empty() || p2.empty())break; int p = p1.front(); int q = p2.front(); p1.pop(); p2.pop(); if (p > q) { p1.push(q); p1.push(p); count++; } else { p2.push(p); p2.push(q); count++; } if (count > 1000) { count = -1; break; } } if (p1.empty() || p2.empty()) { cout << count << " " << (p1.empty() ? 2 : 1) << endl; } else { cout << "-1" << endl; } return 0; }心得:这个题目主要是在找循环那里卡了很久。。。然后就找了下可爱的度娘,看别人用的>1e6.。。。结果自己也跟着用了一下,后来提交时直接在第一个就超时了,过了很久很久(在提交了很多次都在第一个测试就超时了以后)。。。直接把while(t--)去掉,循环次数>1000竟然过了,偶然的发现。也是够了。。。看来以后还得学着点。
相关文章推荐
- hdu 1068 poj 1466 最大独立集
- 数据结构链表——双链表
- Struts2学习记录-Value Stack(值栈)和OGNL表达式
- 从唯一实例谈静态成员
- 数字反转(升级版)
- 2015.7.24 改正后的稍简陋通讯录
- gadget编码与单元测试和service编写示例
- 计数排序、桶排序和基数排序
- java 之CPU~CPU缓存(转)
- bzoj2654
- java 选择排序
- 最新期货交易时间表
- You must supply a layout_width attribute.解决办法
- [整理] 安装完 Ubuntu 15.04 后要做的几件事
- DirectUI界面编程(四)界面布局详解
- DirectUI界面编程(四)界面布局详解
- 我的cocos2之旅 第一天(环境的安装以及Hello World)
- 黑马程序员--面向对象01
- Font Awesome 字体的以及 图标的使用总结
- DirectUI界面编程(四)界面布局详解