您的位置:首页 > 其它

POJ 3480 John (Nim博弈)

2016-09-02 21:53 453 查看
POJ 3480 John

N种糖 每种Ai个,每人每次从中选一种拿
如果每种糖都只有1个,奇数必败偶数必胜
否则,sg = 0败态 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
/*
N种糖 每种Ai个,每人每次从中选一种拿
如果每种糖都只有1个,奇数必败偶数必胜
否则,sg = 0败态
*/
int main()
{
int T;cin>>T;
while (T--){
int s = 0;int n;cin>>n;
bool onlyone = true;
for (int i = 0,x;i < n;++i){
cin>>x;s^=x;
if (x > 1) onlyone = false;
}
if (onlyone){
if (n&1) puts("Brother");
else puts("John");
continue;
}
if (s) puts("John");
else puts("Brother");
}
return 0;
}

java:
//package acm.poj3480;
import java.util.*;
//NIM博弈
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner in = new Scanner (System.in);
int T = in.nextInt();
while ((T--)!=0){
int s = 0;
int n = in.nextInt();
boolean onlyone = true;
for (int i = 0,x;i < n;++i){
x = in.nextInt();
s ^= x;
if (x > 1) onlyone = false;
}
boolean win = true;
if (onlyone&&(n&1)!=0) win = false;
if (!onlyone&&s==0) win = false;
if (win) System.out.println("John");
else System.out.println("Brother");
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  博弈 poj