Euclid's Game

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");
	}
}

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值