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 ( 2n109) 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 Bob Alice 分析:博弈论问题
#include<stdio.h> int num; int main() { int flag,p; while(scanf("%d",&num)!=EOF){ if(num==0) break; flag=1; if(num==2){ printf("Alice\n"); continue; } if(num==3){ printf("Bob\n"); continue; } p=num; for(int i=0;;i++){ if(p%2==0) break; else{ p=(p-1)/2; if(p>3) continue; if(p<=2) break; if(p==3){ printf("Bob\n"); flag=0; break; } } } if(flag) printf("Alice\n"); if(!flag) continue; } return 0; }
参考代码:
#include<set> #include<map> #include<stack> #include<queue> #include<ctime> #include<cmath> #include<vector> #include<string> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define Master Love #define YYF Handsome #define Rp Maxunsignedlonglongint #define Orz zhanhb8 qs1994 #define Designer Zero #define zhx zh[x] #define lth th<<1 #define rth th<<1|1 #define middle (nl+nr)>>1 #define MAX(a,b) (a>b)?a:b #define MIN(a,b) (a<b)?a:b #define getlen(s) strlen(s+1) #define outputans cout<<ans<<endl #define inputint(k) scanf("%d",&k) #define inputchar(c) scanf("%c",&c) #define input64(k) scanf("%I64d",&k) #define inputdouble(k) scanf("%lf",&k) #define inputstring(s) scanf("%s",s+1) #define forout(i,l,r) for(i=l;i<=r;i++) #define forin(i,l,r) for(int i=l;i<=r;i++) #define clearit(a,k) memset(a,k,sizeof(a)) #define formatrix for(int i=1;i<=m;i++)for(int j=1;j<=n;j++) #pragma warning(disable:4996) using namespace std; int T; char s[521]; int sg[101][101] = { 0 }; int val[31] = { 0 }; int main(){ val[0] = 1; for (int i = 1; i <= 30; i++)val[i] = val[i - 1] * 2; int n; while (1){ inputint(n); if (!n)break; bool pos = 0; for (int i = 2; i <= 30; i++)if(val[i]-1==n)pos=1; if (pos)printf("Bob\n"); else printf("Alice\n"); } return 0; }
Box Game