hdu1525——博弈

 

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

 

思路

首先,规定a始终大于b,若a>b&&a<2*b,则只有一种操作a-b,循环直到出现下面的情况。


若a>=2b,则先手总能做到使得b>=1/2a&&a>b,则后手只能有一种操作c=a-b<a-1/2a=1/2a,即操作后a>=2c,又恢复到先手状态了,
先手只需要一直这样循环到出现c=1,或者a%c==0,胜利即可,即先手必胜。

 

代码

#include<iostream>
using namespace std;

int main(){
	int a,b;
	while(cin>>a>>b&&a&&b){
		if(a<b){
			int tmp=a;
			a=b;
			b=tmp;
		}
		bool win=true;
		while(1){
			if(a%b==0||a>2*b){
				break;
			}else{
				int tmp=a;
				a=b;
				b=tmp-b;
				win=!win;
			}
		}
		if(win) cout << "Stan wins" <<endl;
		else cout << "Ollie wins" <<endl;
		
	}
	return 0;
}

 

6266:取石子游戏

描述

有两堆石子,两个人轮流去取.每次取的时候,只能从较多的那堆石子里取,并且取的数目必须是较少的那堆石子数目的整数倍.最后谁能够把一堆石子取空谁就算赢.
比如初始的时候两堆石子的数目是25和7

25 7-->11 7-->4 7-->4 3-->1 3-->1 0
 选手1取 选手2取 选手1取 选手2取 选手1取


最后选手1(先取的)获胜,在取的过程中选手2都只有唯一的一种取法。
给定初始时石子的数目,如果两个人都采取最优策略,请问先手能否获胜。

 

输入

输入包含多数数据。每组数据一行,包含两个正整数a和b,表示初始时石子的数目。
输入以两个0表示结束。

输出

如果先手胜,输出"win",否则输出"lose"

样例输入

34 12
15 24
0 0

样例输出

win
lose
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
bool win(int x,int y)
{
    if (x<y) swap(x,y);
    if (x%y==0 || x/y>=2) return 1;
    return !win(y,x-y);
}
int main()
{
    int x,y;
    while(cin>>x>>y&&x&&y)
      if(win(x,y)) printf("win\n");
      else printf("lose\n");
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值