POJ 3480 John (Nim博弈)

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");
		}
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值