zoukankan      html  css  js  c++  java
  • 队列—summer training B.

    题目描述:

    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
    ",sum);
    66         else printf("%d 1
    ",sum);
    67     }
    68     else 
    69         printf("-1
    ");
    70     
    71     
    72     
    73     return 0;
    74 }

    dy

  • 相关阅读:
    【搞定GTD】如何开始实践GTD?
    【iOS开发笔记21/50】获取应用程序的名称和版本号
    读书笔记:《冠军记忆术》
    【桥牌笔记】绝对不能让东家上手
    OpenInventor开发笔记:解决FaceSet的填充问题
    JasonHelper.Escape 转换字符串为jason格式代码
    在合适的场合使用 with (nolock) 提升查询性能
    嫁给程序员吧!【转】
    美国的工薪族阶层只承担全国"个税”总量的5%
    探秘System.Threading系列 第三篇:Thread的数据ThreadStatic 和LocalDataStoreSlot
  • 原文地址:https://www.cnblogs.com/x512149882/p/4674494.html
Copyright © 2011-2022 走看看