【1020 A Childhood Game(简单博弈论)】

1020 - A Childhood Game
Time Limit: 0.5 second(s) Memory Limit: 32 MB

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.

Sample Input

Output for Sample Input

3

1 Alice

2 Alice

3 Bob

Case 1: Bob

Case 2: Alice

Case 3: Alice


PROBLEM SETTER: JANE ALAM JAN
Developed and Maintained by 
JANE ALAM JAN
Copyright © 2012 
LightOJ, Jane Alam Jan
 
 
 
 
 
 
 
 1 // Project name : 1020 ( A Childhood Game ) 
 2 // File name    : main.cpp
 3 // Author       : iCoding
 4 // E-mail       : honi.linux@gmail.com
 5 // Date & Time  : Sat Aug 11 13:45:21 2012
 6 
 7 
 8 #include <iostream>
 9 #include <stdio.h>
10 #include <string>
11 #include <cmath>
12 #include <algorithm>
13 using namespace std;
14 
15 /*************************************************************************************/
16 /* data */
17 const string Alice = "Alice";
18 const string Bob   = "Bob"  ;
19 
20 /*************************************************************************************/
21 /* procedure */
22 
23 
24 /*************************************************************************************/
25 /* main */
26 int main()
27 {
28     int iT;
29     cin >> iT;
30     for (int iCaseCount = 1; iCaseCount <= iT; iCaseCount++)
31     {
32         int iNum;
33         string iStarter;
34         printf("Case %d: ", iCaseCount);
35         cin >> iNum >> iStarter;
36         if (iStarter == Bob)
37         {
38             /* If Bob starts first, then the player who takes the last marble wins the game. */
39             if (iNum % 3 == 0)
40             {
41                 cout << Alice << endl;
42             }
43             else
44             {
45                 cout << Bob << endl;
46             }
47         }
48         else
49         {
50             /* If Alice starts first, then the player who takes the last marble looses the game. */
51             if ((iNum-1) % 3 == 0)
52             {
53                 cout << Bob << endl;
54             }
55             else
56             {
57                 cout << Alice << endl;
58             }
59         }
60     }
61     return 0;
62 }
63 
64 // end 
65 // Code by Sublime text 2
66 // iCoding@CodeLab 
原文地址:https://www.cnblogs.com/ismdeep/p/2633591.html