POJ 2348 Euclid Game (模拟题)

Euclid's Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7942   Accepted: 3227

Description

Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the first player, subtracts any positive multiple of the lesser of the two numbers from the greater of the two numbers, provided that the resulting number must be nonnegative. Then Ollie, the second player, does the same with the two resulting numbers, then Stan, etc., alternately, until one player is able to subtract a multiple of the lesser number from the greater to reach 0, and thereby wins. For example, the players may start with (25,7):
         25 7

11 7
4 7
4 3
1 3
1 0

an Stan wins.

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

可以参考:《基础训练题解》哈尔滨出版社 俞经善...等编
题意讲解:两个人Ollie和Stan玩一个游戏,每次输入两个非负数。 Stan为先手,Ollie为后手。
每次从大数中减去小数的任意倍数,将其减完后的结果赋值给被减的大数。Ollie刚才的游戏规则,
然后进行循环。直到小数不能再大数中拿到一个非0的数为止。最后一个拿到数的玩家为赢家。

算法讲解:大数除以小数,当值大于1时,正在进行此次游戏的玩家为赢家。否则要继续游戏。
当游戏进行了偶数次的时候,就是Stan赢,否则就是Ollie赢了。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
	int x, y;
	while(scanf("%d %d", &x, &y)!=EOF)
	{
		if(x==0 && y==0) break;

		int dd;
		int cnt=0; //记录游戏进行次数
		int n=x; int m=y;
		while(n!=0 && m!=0 )
		{
			if(m>n){
				cnt++;
				dd = m/n;
				m=m%n; //大数被赋值成那个差值
				if(dd >= 2) break;
			}
			else{
				cnt++;
				dd=n/m;
				n=n%m;
				if(dd>=2 ) break;
			}
		}
		if(cnt%2==1) printf("Stan wins\n");
		else
			printf("Ollie wins\n");
	}
	return 0;
}

 



posted @ 2015-03-14 13:00  我喜欢旅行  阅读(655)  评论(0编辑  收藏  举报