码迷,mamicode.com
首页 > 其他好文 > 详细

队列—summer training B.

时间:2015-07-24 22:31:00      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

题目描述:

Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to nall 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.

案列输入输出:

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

代码如下:
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 using namespace std;
 5 
 6 
 7 int main()
 8 {
 9     int n;
10     scanf("%d",&n);
11     
12     int sum=0;
13     int k1,k2;
14     queue<int>a,b;
15     int s1[10],s2[10];
16     
17     scanf("%d",&k1);
18     int i;
19     for(i=0; i<k1; i++)
20     {
21         scanf("%d",&s1[i]);
22         a.push(s1[i]);
23         
24     }
25     
26     scanf("%d",&k2);
27     for(i=0; i<k2; i++)
28     {
29         scanf("%d",&s2[i]);
30         b.push(s2[i]);
31         
32     } 
33     
34     while(!a.empty()&&!b.empty())
35     {   
36         if(sum<=1000)
37         {
38             int c=a.front();
39             a.pop();
40             int d=b.front(); 
41             b.pop();
42             
43             if(c>d)
44             {    
45                 a.push(d);
46                 a.push(c);
47                 sum++;
48                 
49             }
50             else
51             {
52                 b.push(c);
53                 b.push(d);
54                 sum++;
55             }
56             
57         }
58         else break;
59     }
60     
61     
62     if(sum<=1000)
63     {
64         if(a.empty())
65             printf("%d 2\n",sum);
66         else printf("%d 1\n",sum);
67     }
68     else 
69         printf("-1\n");
70     
71     
72     
73     return 0;
74 }

dy

队列—summer training B.

标签:

原文地址:http://www.cnblogs.com/x512149882/p/4674494.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!