zoukankan      html  css  js  c++  java
  • HDU1907 && HDU2509_Nim博弈变形

    /*
    *题号:1907
    *State: 15MS 268K 572 B C++ *题目大意: * Nim博弈的变形,就是先取完者败。 *解题思路: * 如上题。 * //必输态 S0(考虑奇偶), T2 * //必胜态 S2, S1, T0
    */
    View Code
     1 #include <iostream>
     2 using namespace std;
     3 int main(void)
     4 {
     5     int cas;
     6     scanf("%d", &cas);
     7     while(cas--)
     8     {
     9         int n;
    10         scanf("%d", &n);
    11         int tmp, sum = 0, fullPile = 0;
    12         for(int i = 0; i < n; i++)
    13         {
    14             scanf("%d", &tmp);
    15             if(tmp > 1)
    16                 fullPile = 1;
    17             sum ^= tmp;
    18         }
    19 
    20         if(fullPile == 0)
    21         {
    22             if(sum != 0)
    23             {
    24                 if(n % 2)
    25                     printf("Brother\n");
    26                 else
    27                     printf("John\n");
    28             }
    29             else
    30                 printf("John\n");
    31         }
    32         else
    33         {
    34             if(sum == 0)
    35                 printf("Brother\n");
    36             else
    37                 printf("John\n");
    38         }
    39     }
    40     return 0;
    41 }

     详细介绍该种算法:http://www.cnblogs.com/cchun/archive/2012/07/25/2609141.html

    /*
    *题号:2509
    *State: 0MS 268K 510 B C++ *题目大意; * 给定n堆苹果,每次任意取一堆中的n个,最先取完者败 * 判断先手输赢。 *解题思路: * 利用Nim博弈中的变形来得到。 */
    View Code
     1 #include <iostream>
     2 using namespace std;
     3 int main(void)
     4 {
     5     int n;
     6     while(scanf("%d", &n) == 1)
     7     {
     8         int tmp, sum = 0, fullPile = 0;
     9         for(int i = 0; i < n; i++)
    10         {
    11             scanf("%d", &tmp);
    12             if(tmp > 1)
    13                 fullPile = 1;
    14             sum ^= tmp;
    15         }
    16 
    17         //必输态 S0(考虑奇偶), T2
    18         //必胜态 S2, S1, T0
    19 
    20         if(!fullPile)
    21         {
    22             if(sum != 0)
    23             {
    24                 if(n & 1)
    25                     printf("No\n");
    26                 else
    27                     printf("Yes\n");
    28             }
    29             else
    30                 printf("Yes\n");
    31         }
    32         else
    33         {
    34             if(sum == 0)
    35                 printf("No\n");
    36             else
    37                 printf("Yes\n");
    38         }
    39     }
    40     return 0;
    41 }
  • 相关阅读:
    mac zsh选择到行首的快捷键
    phalcon下拉列表
    tinycore remaster方法
    bundle安装方法
    centos7安装avahi
    pydoc介绍
    macosx下apache的默认用户为daemon
    centos配置ssh免密码登录后,仍提示输入密码
    xampp默认项目文件夹htdocs
    微信开发:"errcode": -1000,"errmsg": "system error"错误的解决办法
  • 原文地址:https://www.cnblogs.com/cchun/p/2609186.html
Copyright © 2011-2022 走看看