zoukankan      html  css  js  c++  java
  • C

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    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.

    Sample Input

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

    Hint

    First sample:

    Second sample:

    题意:

    两个人打牌,共有n张各不相同的牌,两人分别有k1和k2张,每人从牌堆顶取一张牌对比,牌大的一方分别将对方牌放入牌堆底,再将自己出的牌放入牌堆底。如此进行直到某一方牌堆为空。如果不能结束则输出-1.

    典型的队列应用嘛~说来惭愧队列的基本操作都忘得差不多了,做题时还腆着脸地去看了一下以前写的博客。。

    按照要求入队出队就可以了,还有需要注意的就是开始输入时的顺序,千万不要先将k1、k2输完再循环输入各自的牌值。

    还有一件事,判断死循环的话实在是太麻烦了!!!在此处我取了个巧,将循环次数>100000全部判定为死循环,意料之中的A了 ~  不要打我【抱头

    附AC代码:

     1 #include<iostream>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<queue>
     5 using namespace std;
     6 
     7 int main(){
     8     int n,k1,k2,x;
     9     cin>>n>>k1;//注意是k1、k2分开输 
    10     queue<int> l;
    11     queue<int> r;
    12     for(int i=0;i<k1;i++){//入队 
    13         cin>>x;
    14         l.push(x);
    15     }
    16     cin>>k2;
    17     for(int i=0;i<k2;i++){
    18         cin>>x;
    19         r.push(x);
    20     }
    21     //cout<<r.front()<<" "<<l.front()<<endl;
    22     int ans=0;
    23     while(1){
    24         if(l.front()>r.front()){//当1大时按顺序分别将r的队顶元素和l的队顶元素插入l队尾 
    25             l.push(r.front());
    26             l.push(l.front());
    27             l.pop();//注意弹出已插入的元素 
    28             r.pop();
    29             ans++;
    30         }
    31         else if(l.front()<r.front()){//同上 
    32             r.push(l.front());
    33             r.push(r.front());
    34             r.pop();
    35             l.pop();
    36             ans++;
    37         }
    38         if(l.empty()){//若l为空时,2 win 
    39             cout<<ans<<" "<<"2"<<endl;
    40             return 0;
    41         }
    42         if(r.empty()){
    43             cout<<ans<<" "<<"1"<<endl;
    44             return 0;
    45         }
    46         if(ans==100000){//取巧了 哈哈 
    47             cout<<"-1"<<endl;
    48             return 0;
    49         }
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    vue-cli 2.x升级到3.x版本, 和3.x降级到2.x版本命令
    vue-cli 2.x项目,删除打包线上环境的控制台打印
    vue-cli 2.x项目使用cross-env新建多个打包环境
    解决vue项目路由出现message: "Navigating to current location (XXX) is not allowed"的问题
    vue打包后,解决出现不到字体文件的错误
    js 网络图片转base64的方式(两种)
    vscode编译器,Settings Sync 同步插件,忘记GitHub token 和 Gist的解决办法
    输入两个时间,计算他们相差多少天多少时,多少秒
    老生常谈之js深拷贝与浅拷贝
    React Hooks 你不来了解下?
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/5711802.html
Copyright © 2011-2022 走看看