LightOJ-1020-A Childhood Game(博弈)
阅读原文时间:2023年07月11日阅读:4

链接:

https://vjudge.net/problem/LightOJ-1020

题意:

Alice and Bob are playing a game with marbles; you may have played this game in childhood. The game is playing by alternating turns. In each turn a player can take exactly one or two marbles.

Both Alice and Bob know the number of marbles initially. Now the game can be started by any one. But the winning condition depends on the player who starts it. If Alice starts first, then the player who takes the last marble looses the game. If Bob starts first, then the player who takes the last marble wins the game.

Now you are given the initial number of marbles and the name of the player who starts first. Then you have to find the winner of the game if both of them play optimally.

思路:

考虑最后拿输,则2,3都是必赢,得到n%3 == 1先手输。

最后拿赢,则1,2必赢,n%3 == 0先手输。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>

using namespace std;
typedef long long LL;
const int INF = 1e9;

const int MAXN = 1e6+10;
const int MOD = 1e9+7;

int main()
{
    int t, cnt = 0;
    LL n;
    char s[10];
    scanf("%d", &t);
    while(t--)
    {
        scanf("%lld%s", &n, s);
        printf("Case %d: ", ++cnt);
        if (s[0] == 'A')
        {
            if (n%3 == 1)
                puts("Bob");
            else
                puts("Alice");
        }
        else
        {
            if (n%3 == 0)
                puts("Alice");
            else
                puts("Bob");
        }
    }

    return 0;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章