Title Link: Box Game
Surface:
12293 Box GameThere is identical boxes. One of them contains n balls, while the other box contains one ball.
Alice and Bob invented a game with the boxes and balls, which is played as follows:
Alice and Bob moves Alternatively, Alice moves first. For each move, the player finds out the box
Have fewer number of balls inside, and empties that box (the balls inside would be removed forever),
And redistribute the balls in the other box. After the redistribution, each box should contain at least
One ball. If A player cannot perform a valid move, he loses. A Typical game is shown below:
When the both boxes contain only one ball, Bob cannot does anything more, so Alice wins.
Question:if Alice and Bob is both clever enough, who'll win? Suppose both of them is very
Smart and always follows a perfect strategy.
Input
There'll is at the most test cases. Each test case contains an integer n (2≤n≤109) in a single
Line. The input terminates by n = 0.
Output
For each test case, print a, the name of the winner.
Sample Input
2
3
4
0
Sample Output
Alice
Bob
Alice
Solving:
Game basically do not, this problem is very good push, first simulation found 1,3,7 as must lose state, as long as the latter situation can let decomposition into the previous must lose the state to Bob so all is Alice win, it is easy to launch the next must lose 15. Because at this time the smallest can only get 8. This problem can also find the law, 1,3,7,15,31 ... always 2^n-1.
Code:
#include <iostream>using namespace Std;int main () {int N;while (cin>>n&&n) { bool flag=true; while (n) { if ((n-1)%2) { flag=false; break; } n= (n-1)/2; } if (flag) cout<< "bob\n"; else cout<< "alice\n";}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA 12293 Box Game (game starter)