Programming Challenges College Club number game answer

Source: Internet
Author: User
Tags int size
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 (); }
    
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.