Question D: Euclid's Game
Time limit: 1 Sec memory limit: MB
Submissions: 27 Resolution: 6
Submitted State [Discussion Version]
Title Description
Xiao Ming and Xiao Hong are playing Euclid's game. They start with two natural numbers, the first player xiaoming, from the larger number of two numbers minus the largest possible positive integer multiple, as long as the difference is non-negative. Then, the second player is red, to get the two number of the same operation, and then again xiaoming. In turn, the game is played, until a player will subtract a larger number from a multiple of the smaller number after the difference is 0, when the game is over, the player is the winner.
Input
The input contains multiple sets of test data. Each group enters two positive integers, which means that the game starts with two numbers, and the game is always started by Xiaoming first.
When you enter two 0, the input ends.
Output
For each set of inputs, the final winner of the output, we think that both of them are top players, each step of the game has made the best choice.
The output format is shown in the output example.
Sample input
34 12
15 24
0 0
Sample output
Xiaoming wins
Xiaohong wins
Tips
Submitted State [Discussion Version]
Problem Solving Ideas:
This is the game type of the topic, if the outcome is not doomed, then who will master the game of the initiative, that is, who ultimately wins.
Problem Solving Code:
#include <stdio.h>
int main ()
{
int a,b,k,t;
while (scanf ("%d%d", &a,&b), a!=0| | b!=0)
{
k=1;
if (a==b)
{
printf ("Xiaoming wins\n");
}
else
{
while (a!=b)
{
if (a<b)
{
t=a;
a=b;
b=t;
}
k++;
if (a/b!=1)
{break
;
}
a=a%b;
}
if (k%2==1)
printf ("Xiaohong wins\n");
else
printf ("Xiaoming wins\n");
}
}
return 0;
}
Error Resolution:
The same situation is not considered for the initial value of the two people.