HDU 1907 John (Nim博弈 模板)

来源:互联网 发布:最短路径的算法 编辑:程序博客网 时间:2024/05/21 14:07

题意:约翰和他哥哥玩一个游戏,n堆糖果,每堆糖果Ai个,约翰先拿任意个同一种的糖果,然后轮到他的哥哥拿任意个同一种的糖果。谁最后拿完最后一堆的最后一个糖果,谁获胜。

Nim博弈的模板题目。






#include <iostream>#include <algorithm>#include <cmath>#include <cstdio>using namespace std;int main(){   int t;   scanf("%d",&t);   while(t--)   {       int n,num=0,a[50],flag=0;       scanf("%d",&n);       for(int i=1;i<=n;i++){        scanf("%d",&a[i]);        if(a[i]!=1)            num++;       }       if(num==0)//第一种情况       {           if(n%2==0)            flag=1;       }       else if(num==1) //第二种情况        flag=1;       else            //第三种情况       {           int nim=a[1];           for(int i=2;i<=n;i++)            nim=nim^a[i];          if(nim!=0)            flag=1;       }       printf("%s\n",flag==1?"John":"Brother");   }   return 0;}



原创粉丝点击