Solution: Euclid game (Game)

Title Description
Bob and Alice are playing the game Euclid. They started with two natural numbers, the first player Xiao Ming, subtract the smaller number of positive integer times as large as possible from the larger number of two numbers, as long as the difference can be non-negative. Then, the second player red, two numbers was subjected to the same operation, and then a small described. So turn the game until a player after a larger number minus some multiple of the smaller number of difference reaches zero, then the game ends, the player is the winner. Input test data comprising a plurality of sets. Each set of input two positive integer representing the number two game begins, the game always Xiaoming first start.

When the two inputs 0, the input end.
Output For each input, output the final winner, we think they are both top players, every step of the game have made the best choice.

Specific output formats, see sample output.
Sample input
34 is 12 is
15 24
0 0
Sample Output
Xiaoming WINS
Xiaohong WINS
idea source
win point, it can have a state transition losing points;
losing point, it can transfer the status of winning points are ;
no intermediate state;
can be divided into three cases m is a large number to a smaller number n
when m% n == 0 when:
win
when m / n is equal to 1 only one emulated, then down to a required state transfer judgment
(m, n) -> ( n, m% n)
when m / n> = 2 who is who wins taken:
since when the (n, m% n) is doomed to failure state
taken may be taken as (m% n + n, n )
is transferred to another person losing state the contrary, if (m% n + n, n ) is doomed to failure state
taken were taken as (n, m% n)
this is also called the upper hand advantage

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
 int n,m;
 bool p;
 while(cin>>n>>m&&n&&m)
 {
  p=1;
  while(1)
  {
   if(n<m) swap(n,m);
   if(n%m==0||n-m>m) break;
   n-=m;p=!p;
  }
  if(p) cout<<"xiaoming wins"<<endl;
  else cout<<"xiaohong wins"<<endl;
 }
 return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43540515/article/details/91050207