zoukankan      html  css  js  c++  java
  • 打牌~~~

    题意:

    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.

     

    Input

    4 
    2 1 3
    2 4 2
    Output
    6 2

    思路:
    用队列模拟两个玩家打牌,比较两个玩家最顶上的大小,一直到有玩家没牌了。还有一种就是平手,直接设置为循环1000次之后还没出结果就是循环。

    源代码:
     1 #include<iostream>
     2 #include<queue>
     3 #include<cstring>
     4 using namespace std;
     5 queue<int>p1, p2;
     6 int main()
     7 {
     8     int t;
     9     cin >> t;
    10     int count = 0;
    11     int a1, a2;
    12     int i = 0;
    13     int n;
    14     int  m;
    15     cin >> n;
    16     for (i = 0; i<n; i++)
    17     {
    18         cin >> a1;
    19         p1.push(a1);
    20     }
    21     cin >> m;
    22     for (i = 0; i<m; i++)
    23     {
    24         cin >> a2;
    25         p2.push(a2);
    26     }
    27     while (1)
    28     {
    29 
    30         if (p1.empty() || p2.empty())break;
    31         int p = p1.front();
    32         int q = p2.front();
    33         p1.pop();
    34         p2.pop();
    35 
    36         if (p > q)
    37         {
    38             p1.push(q);
    39             p1.push(p);
    40             
    41             count++;
    42         }
    43         else
    44         {
    45             p2.push(p);
    46             p2.push(q);
    47             
    48             count++;
    49         }
    50 
    51         if (count > 1000)
    52         {
    53             count = -1;
    54             break;
    55 
    56         }
    57     }
    58 
    59     if (p1.empty() || p2.empty())
    60     {
    61 
    62         cout << count << " " << (p1.empty() ? 2 : 1) << endl;
    63     }
    64     else
    65     {
    66         cout << "-1" << endl;
    67     }
    68 
    69     return 0;
    70 }

    心得:

         这个题目主要是在找循环那里卡了很久。。。然后就找了下可爱的度娘,看别人用的>1e6.。。。结果自己也跟着用了一下,

    后来提交时直接在第一个就超时了,过了很久很久(在提交了很多次都在第一个测试就超时了以后)。。。直接把while(t--)去掉,循环次数>1000竟然过了,偶然的发现。

    也是够了。。。看来以后还得学着点。







    ------------------------ 没有谁的人生不是斩棘前行 ---------------------------------------- JM
  • 相关阅读:
    Tomcat基于MSM+Memcached实现Session共享
    Zabbix简介及安装
    redis简介
    Ansible详解(二)
    Ansible详解(一)
    WAMP3.1.10/Apache 设置站点根目录
    最长回文子串--轻松理解Manacher算法
    一篇文章彻底了解Java垃圾收集(GC)机制
    java内存模型详解
    Java中23种设计模式--超快速入门及举例代码
  • 原文地址:https://www.cnblogs.com/Lynn0814/p/4675017.html
Copyright © 2011-2022 走看看