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");
}
}
}
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 3480 John [博弈之Nim 与 Anti-Nim]
- Anti-nim博弈 John poj3480
- POJ 3480 & HDU 1907 John(尼姆博弈变形)
- Anti_SG博弈,SJ定理_______John( POJ 3480 )
- POJ 3480 John Anti-Nim博弈变形
- POJ 3480 & HDU 1907 John(尼姆博弈变形)
- poj 3480 John 简单的nim博弈
- POJ Georgia and Bob(Nim博弈)
- 【博弈】poj 2960 S-Nim
- poj2960 S-Nim(博弈SG函数)
- POJ - 1704 Georgia and Bob 神TM NIM博弈,脑洞,神分析
- HDU 1907 John 与 poj 3480
- poj 1704 Georgia and Bob nim博弈
- (hdu 1907)John (Misère Nim,Nim博弈的变形)
- dp博弈______Nim( poj 2068 )
- poj 2068 Nim(博弈dp)
- poj 2068 NIM (博弈DP)
- poj2960【Nim博弈,SG】
- POJ 1704 Georgia and Bob (Nim博弈)
- poj1704 Georgia and Bob(Nim变形:阶梯博弈)