HDU 1907 ACM博弈论入门:尼姆博奕

理解尼姆博奕之后,这道题只需要考虑全为1的特殊情况,剩下的就是两种结果反转了。

代码如下: 在GitHub上查看

#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    for(int i=0;i<t;i++)
    {
        int n;
        scanf("%d",&n);
        int ans=0;
        int tmp;
        //ans--;
        bool have_over_one=false;
        for(int j=0;j<n;j++)
        {
            scanf("%d",&tmp);
            if(tmp>1)
            {
                have_over_one=true;
            }
            ans=ans^tmp;
        }
        #if 0
        printf("%d\n",ans);
        #endif // 0
        if(!have_over_one)
        {
            if(n%2==0)
            {
                printf("John\n");
            }
            else
            {
                printf("Brother\n");
            }
        }
        else
        {
            if(ans==0)
            {
                printf("Brother\n");
            }
            else
            {
                printf("John\n");
            }
        }
    }
    return 0;
}

我在GitHub上建立了一个仓库,用于存放已经AC的题目的源代码。如果各位有未收录的题目或者有更好的解法,欢迎fork仓库+PR~ 让我们共同创建一个AC代码集中仓库,造福ACM Beginner ~

仓库地址: OJ-Problems-Source On GitHub

你可能感兴趣的:(尼姆博奕)