SDNU 51nod 几道简单的博弈题目

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 【威佐夫博弈+大数+高精度】

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值