Euclid's Game
有两个玩家,Stan 和 Ollie, 在玩游戏。初始有两个自然数。Stan是先手,每次把大的数字减去小的数字的任意倍数,但是不能使数字变成负数。然后Ollie进行同样的操作,直到有一个玩家使一个数字变为零。
例如,初始时数字为(25,7):
25 7
11 7
4 7
4 3
1 3
1 0
这样Stan赢
Input
The input consists of a number of lines. Each line contains two positive integers giving the starting two numbers of the game. Stan always starts.
Output
For each line of input, output one line saying either Stan wins or Ollie wins assuming that both of them play perfectly. The last line of input contains two zeroes and should not be processed.
Sample Input
34 12
15 24
0 0
Sample Output
Stan wins
Ollie wins
解题思路:
我们可以发现当两数相差在两倍以上时会出现这种情况:
若a=2*b+k(k<b);
此时按规则来会出现两种情况,1.a变成了k,2.a变成了b+k,我们可以发现当
情况1赢的时候我们一定选择情况1,而如果情况1不会赢,那么此时我们选择情
况2会发现,在我们将a变成b+k后,后手只能把a变成k这一个操作,正好就是情
况一,但是我们变成了后手,那么当我们直接选择情况1会输的话,此时就一定会
赢,所以当两数相差二倍以上,这是一个必胜态,而当两数相差在一倍与二倍之间
就是一个简单模拟的过程,代码如下:
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
int a , b;
while(scanf("%d %d", &a, &b))
{
if(a == 0 && b == 0)
return 0;
int mi = min( a, b);
int ma = max( a, b);
int flag = 0;
while(ma)
{
if(ma % mi == 0 || ma / mi >= 2)//能够获胜的两种情况
break;
else
{
ma = ma - mi;
int t = ma;
ma = mi;
mi = t;
flag = !flag;
}
}
if(flag == 0)
printf("Stan wins\n");
else
printf("Ollie wins\n");
}
}