poj3480--John

题意:n堆石子,两人轮流操作,每次操作只能选定其中一堆,并取走若干个(>=1个)。谁取走最后一个谁输。给定一个状态,问先取的赢还是后取的赢。

整个游戏反过来,如果sg为0先手必胜,不为0必败。(特殊情况:所有堆都是1,这时候奇数堆必胜,偶数必败)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<string>
 6 #include<algorithm>
 7 int main(){
 8     int T,n,x;
 9     scanf("%d",&T);
10     while (T--){
11         int cnt=0,sg=0;
12         scanf("%d",&n);
13         for (int i=1;i<=n;i++){
14             scanf("%d",&x);
15             if (x==1) cnt++;
16             sg^=x;
17         }
18         if (cnt==n){
19             if (sg) printf("Brother
");
20             else printf("John
");
21         }
22         else{
23             if (sg) printf("John
");
24             else printf("Brother
");
25         }
26     }
27 }
原文地址:https://www.cnblogs.com/qzqzgfy/p/5266823.html