51nod 1069 Nim游戏

来源:互联网 发布:共识网 知乎 编辑:程序博客网 时间:2024/05/29 18:40

题目:

1069 Nim游戏
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
 收藏
 关注
有N堆石子。A B两个人轮流拿,A先拿。每次只能从一堆中取若干个,可将一堆全取走,但不可不取,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N及每堆石子的数量,问最后谁能赢得比赛。
例如:3堆石子,每堆1颗。A拿1颗,B拿1颗,此时还剩1堆,所以A可以拿到最后1颗石子。
Input
第1行:一个数N,表示有N堆石子。(1 <= N <= 1000)第2 - N + 1行:N堆石子的数量。(1 <= A[i] <= 10^9)
Output
如果A获胜输出A,如果B获胜输出B。
Input示例
3111
Output示例
A
相关问题
Bash游戏 V2 
10
 
Bash游戏 
0
 
威佐夫游戏 V2 
0
 
威佐夫游戏 
0
 
Bash游戏 V4 
40
Bash游戏 V3 
20
这个题不知道怎么推的,只知道一个个做异或运算,结果是0 的话B赢,否则A赢

代码:

#include <cstdio>  #include <iostream>  #include <cmath>  #include <cstring>  #include <queue>  using namespace std ;   int const maxn = 1005 ;  int a[maxn];    int main()  {      int n;      while(cin>>n)      {           int ans = 0;           for(int i = 0 ; i < n ;i++)          {              cin>>a[i];              ans^=a[i];          }          if(ans==0)puts("B");          else puts("A");        }      return 0;  }


0 0