Topic details
Two people play a number game, given two positive integers a, B, two people take turns from one number minus the positive number of the other number, to ensure that the result is non-negative, first get 0 wins. For example: 30 8 can be changed to 22 8 or 14 8 or 6 8 in one step. Two of people are smart enough.
Input format:
Multiple sets of data, each with a row of two space-delimited positive integers (within the 32-bit entire book range)
Output format:
Each group of data row, the output is the first person to win, or the second person to win.
Answer Instructions
Input sample
3 5
4 5
Sample output:
1
2
The code is as follows
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Scanner;
public class Mathematics {public static void main (string[] args) {Scanner cin = new Scanner (system.in); int min = 0;//smaller int max = 0;//larger number while (Cin.hasnext ()) {min = Cin.nextin
T ();
max = Cin.nextint ();
if (min > max) {int temp = min;
min = max;
max = temp;
} if (max% min = = 0) {System.out.println (1);//first person wins continue;
} list<integer> queue = new arraylist<integer> ();
while (true) {int Shang = max/min;
int mod = max% min;
if (mod = = 0) break;
Queue.add (Shang);
if (min > MoD) {int temp = min; min = mod;
MoD = temp;
} max = mod;
} int size = Queue.size ();
if (size = = 1) {if (queue.get (0) = = 1) {System.out.println (2);//second person wins
} else {System.out.println (1);//First person wins} continue;
} int win =-1;
for (int i = size-1; I >= 0; i--) {if (i = = size-1) {if (Queue.get (i) = = 1) {
Win = 1;
} else {win = 2;
}} else if (i = = 0) {if (win = = 2 && queue.get (0) > 1) {
Win = 1; }} else {if (queue.get (i) == 1) {win = win = = 1? 2:1;
} else {win = 2;
}}} System.out.println (Win);
} cin.close (); }
}