HDU 1525 Euclid's Game (博弈)

,谢谢 http://blog.csdn.net/ACM_cxlove?viewmode=contents           by---cxlove

给出两个数,a和b,将大的数中,减去若干b的倍数,最终有一个数为0的话就胜了。

http://acm.hdu.edu.cn/showproblem.php?pid=1525

两个数a和b,总会出现的一个局面是b,a%b,这是必然的,如果a>=b&&a<2*b,那么只有一种情况,直接到b,a%b。否则有多种情况。

对于对于a/b==1这种局面,只可能到b,a%b,没有选择。而a/b>=2的话,先手可以选择由谁面对b,a%b这样的局势

显然选手足够聪明b,a%b谁必胜必败已经清楚,先手在a/b>=2的局面必胜

感悟  :不要一味着去发现完美规律 适当的将简单运算让计算机完成 会是一片光明

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
bool judge(int a,int b)
{
	if(a%b==0) return 1;
	if(a>=b*2) return 1;
	int count =0;
	while(a%b&&a<b*2&&b!=1)
	{
		swap(a,b);
		b-=a;
		count++;
	}
	if(count%2==0) return 1;
	else return 0;
}
int main()
{
    int a,b;
    while(cin>>a>>b&&a&&b)
    {
    	if(a<b) swap(a,b);
		if(judge(a,b)) cout<<"Stan wins\n";
		else cout<<"Ollie wins\n";
	}
}

猜你喜欢

转载自blog.csdn.net/ty_gyy/article/details/80097544