1.51nod 1066 Bash游戏
最基本的巴什博奕,告诉你n和k,当n%(k+1) == 0时后手必胜,否则先手必胜。
Mycode:
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1005;
int main()
{
int T;
int n, k;
cin >> T;
while(T--)
{
cin >> n >> k;
if(n % (k + 1) == 0)
puts("B");
else
puts("A");
}
return 0;
}
2.SDNU 1499 Problem_H
Bash博弈+大数 这里k=4
Mycode:
import java.math.BigInteger;
import java.math.BigDecimal;
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int T;
BigInteger n;
BigInteger MOD = new BigInteger("5");
T = scanner.nextInt();
while((T--) > 0)
{
n = scanner.nextBigInteger();
if(n.mod(MOD).compareTo(BigInteger.ZERO) == 0)
System.out.println("chaochao");
else
System.out.println("huahua");
}
}
}
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1005;
int main()
{
int T;
char ch;
string s;
cin >> T;
while(T--)
{
cin >> s;
ch = s[s.size() - 1];
if(ch == '5' || ch == '0')
puts("chaochao");
else
puts("huahua");
}
return 0;
}
3.51nod 1069 Nim游戏
最基本的nim博弈,当所有值^后的结果 == 0时,后手必胜,否则先手必胜。
Mycode:
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1005;
int main()
{
int t;
int tem, ans = 0;
cin >> t;
while(t--)
{
cin >> tem;
ans ^= tem;
}
if(ans == 0)
puts("B");
else
puts("A");
return 0;
}
4.51nod 1072 威佐夫游戏
最基本的wythoff博弈,x为x、y中较大的那个值,当floor((sqrt(5.0)+1)/2 * (x-y)) == y时,后手必胜,否则先手必胜。
Mycode:
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1005;
double gold;
int main()
{
gold = (sqrt(5.0) + 1) / 2;
int t;
int n, m;
cin >> t;
while(t--)
{
scanf("%d%d",&n,&m);
if(n > m)
swap(n, m);
int w = gold * (m-n);
if(w == n)
puts("B");
else
puts("A");
}
return 0;
}
5. 51nod 1185 威佐夫游戏 V2
wythoff博弈的高精度算法。
因为黄金分割数的精度问题,所以当x、y取较大数时,对它的精度也有了相应的要求。
这里JAVA的代码可见另一篇更高精度的博客:2016 ICPC 大连 C Game of Taking Stones 【威佐夫博弈+大数+高精度】