There are two 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
having fewer number of balls inside, and empties that box (the balls inside will 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 both boxes contain only one ball, Bob cannot do anything more, so Alice wins.
Question: if Alice and Bob are both clever enough, who will win? Suppose both of them are very smart and always follows a perfect strategy.
Input There will be at most 300 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 single line, the name of the winner.
Sample Input
2
When both boxes contain only one ball, Bob cannot do anything more, so Alice wins.
Question: if Alice and Bob are both clever enough, who will win? Suppose both of them are very smart and always follows a perfect strategy.
Input There will be at most 300 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 single line, the name of the winner.
Sample Input
2
3
4
0
Sample Output
Alice
Sample Output
Alice
Bob
Alice
题意:有两个箱子,一个里面有n个球,另一个有1个球。然后A(alice)和B(Bob)做游戏。游戏规则是:
两者轮流操作,A先开始:先将 球比较少 的箱子倒空,再将另一个箱子里的球分配到空箱子里,直到两个箱子都只有一个球。最终不能操作的人,输掉比赛。
思路:找规律 ,见下表;(n*2)+1
n值 | 赢的人 |
N值
| 赢的人 |
2 | A | 21 | A |
3 | B | 22 | A |
4 | A | 23 | A |
5 | A | 24 | A |
6 | A | 25 | A |
7 | B | 26 | A |
8 | A | 27 | A |
9 | A | 28 | A |
10 | A | 29 | A |
11 | A | 30 | A |
12 | A | 31 | B |
13 | A | 32 | A |
14 | A | ||
15 | B | ||
16 | A | ||
17 | A | ||
18 | A | ||
19 | A | ||
20 | A |
|
代码如下:
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
char mm[1000000005];
int main()
{
long long ans=7;
mem(mm,'A');
mm[3]=mm[7]='B';
for(int i=1;i<=25;i++)
{
ans=(ans*2)+1;
// printf("%lld\n",ans);
mm[ans]='B';
}
int n;
while(~scanf("%d",&n)&&n)
{
if(mm[n]=='B')
printf("Bob\n");
else
printf("Alice\n");
}
}
#include<stdio.h>
#include<algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
char mm[1000000005];
int main()
{
long long ans=7;
mem(mm,'A');
mm[3]=mm[7]='B';
for(int i=1;i<=25;i++)
{
ans=(ans*2)+1;
// printf("%lld\n",ans);
mm[ans]='B';
}
int n;
while(~scanf("%d",&n)&&n)
{
if(mm[n]=='B')
printf("Bob\n");
else
printf("Alice\n");
}
}