算法——Euclid's Game

题目描述

Starts with two unequal positive numbers (M,N and M>N) on the board. Two players move in turn. On each move, a player has to write on the board a positive number equal to the difference of two numbers already on the board; this number must be new, i.e., different from all the numbers already on the board. The player who cannot move loses the game. Should you choose to move first or second in this game?

According to the above rules, there are two players play tihs game. Assumptions A write a number on the board at first, then B write it.

Your task is write a program to judge the winner is A or B.

输入

Two unequal positive numbers M and N , M>N (M<1000000)

输出

A or B

样例输入

3 1

样例输出

A

代码

#include<iostream>
using namespace std;
int main(){
	int n, m;
	cin>>n>>m;
	char win = 'B';
	if(n % (m + 1) != 0){
		win = 'A';
	}
	cout<<win<<endl;
	return 0;
} 

思路

1.题没有说明白 M ,N 具体代表什么,其中M : 两个人谁先写到这个数,则胜利,N :新写的数,对于前一个数来说,不得超过的范围。
2.题很简单,掌握其中的规律即可:当且仅当M 不是 N+1 的倍数,对先手来说则是一个胜局。
3.胜利的策略是在每次拿走 M mod (N+1) 个棋子;背离这个策略,则会把胜局留给对方。

发布了10 篇原创文章 · 获赞 96 · 访问量 5792

猜你喜欢

转载自blog.csdn.net/qq_45703420/article/details/105196557