码迷,mamicode.com
首页 > 其他好文 > 详细

Light OJ 1020 - A Childhood Game

时间:2018-03-05 20:51:00      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:each   最优   else   循环   tar   pen   size   depends   find   

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.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains an integer n (1 ≤ n < 231) and the name of the player who starts first.

Output

For each case, print the case number and the name of the winning player.

 

Alice和Bob在用弹珠玩游戏。你可能儿时玩过这个游戏。它是一个轮流进行的游戏。每一轮一个人可以拿走一或两个弹珠。Alice和Bob都知道弹珠的初始数目。现在游戏可以从任何人开始。但游戏的规则取决于第一个拿弹珠的人。
如果第一个拿弹珠的人是Alice,最后一个拿完弹珠的人失败。
如果第一个拿弹珠的人是Bob,最后一个拿完弹珠的人胜利。
现在给你弹珠的初始数量,和第一个拿弹珠的人。
请你找出他们都做最优操作时,胜利的人是谁。
 
 
思路:通过每个规则的必胜态找规律,发现是3个数一循环。
 
代码:
#include <cstdio>
#include <cstring>

int main()
{
    int T,Case=1,n;
    char Tname[10];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %s",&n,Tname);
        if(Tname[0]==‘A‘)
        {
            if(n%3==1)
            {
                printf("Case %d: Bob\n",Case++);
            }
            else
            {
                printf("Case %d: Alice\n",Case++);
            }
        }
        else
        {
            if(n%3==0)
            {
                printf("Case %d: Alice\n",Case++);
            }
            else
            {
                printf("Case %d: Bob\n",Case++);
            }
        }
    }
    return 0;
}

 

Light OJ 1020 - A Childhood Game

标签:each   最优   else   循环   tar   pen   size   depends   find   

原文地址:https://www.cnblogs.com/zhangzehua/p/8510857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!