51Nod 1069 Nim游戏 (位运算)

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1069

有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示例
3
1
1
1
Output示例
A

题解:异或 位运算
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <string>
 6 #include <cmath>
 7 using namespace std;
 8 #define ll long long
 9 const int N=10005;
10 int main()
11 {
12     int n,s=0;
13     cin>>n;
14     while(n--){
15         int m;
16         cin>>m;
17         s^=m;
18     }
19     if(s!=0) cout<<"A"<<endl;
20     else cout<<"B"<<endl;
21     return 0;
22 }
原文地址:https://www.cnblogs.com/wydxry/p/7349270.html