The Java implementation of the Joseph question (drawing on someone else's code + your own analysis of the understanding)

Source: Internet
Author: User
Tags sca

Http://www.blogjava.net/rorely/archive/2010/01/15/309732.html

Original Blog Address

ImportJava.util.Scanner;/*** Use an array to implement the Joseph ring problem * The circle count is enclosed by a m-person. * Starting from the first person, count off from 1, check in n people out of the circle, * the remainder continues to count off from 1 until all the people are out of the loop. * For a given m and N, find out the order of all the rings.*/ Public classringyuesefu{ Public Static voidMain (string[] args) {System.out.println ("The procedure is described as follows:"); System.out.println ("The M-person is surrounded by a circle that is connected to the end." Starting from the first person, starting from 1, the number of people reporting n out of the circle, the rest of the people continue to count from 1, until all the people out of the circle so far. For a given M and N, find out the order in which everyone is out of the loop.); //Prompt to enter total number of peopleSystem.out.println ("Please enter the total number of people doing this game:"); Scanner SCA=NewScanner (system.in); intm=Sca.nextint (); //prompt to enter a value to be circledSystem.out.println ("Please enter a value to be out of the loop:"); intn=Sca.nextint (); System.out.println ("Output sequence number in the Order of the Rings:"); //create an array with M values        int[] A=New int[M]; //The initial length, after the ring one, the length is reduced by one        intlen=m; //Assigning a value to an array         for(inti=0;i<a.length;i++) A[i]=i+1; //I is the element below table, J represents the number of current to be reported        intI=0; intJ=1; //start the game, as long as someone in the circle to continue the game, judged as len>0, each out of a person len--until the end of 0 games. //but the length of the array does not change, the value of the array element is changed, the value of the loop is-1. All of the last array values are-1, which means everyone is out of the loop.         while(len>0){            //The game has two conditions, the first to ensure that the position of someone, that is, a[] is not 1, the reason is not 1 because the back to the position of the circle into 1//The reason for using a[i%m] is to prevent the array angle label out of bounds, such as M=10,i is from 0 to 9 this is a lap, and then i++ 10, 10%10=0,//The IF is not executed, but else I++,i is 11%10>0 to continue. Prevent a[10] Cross-border situations            if(a[i%m]>0){                if(j%n==0) {//find the person who wants to go out and reduce the number of people in the circle by oneSystem.out.print (a[i%m]+ ""); A[i%m]=-1; J= 1;//someone's out of the loop, starting from 1 .i++; Len--; }Else{//no one out of the circle, i++, Position backward Move, J + + slogan plus one, such as the report is 1, the next 2,3,4 until Ni++; J++; }            }Else{//when the position is 1, it jumps to the next i++, but J does not add one .i++;        }} System.out.println ();     System.out.println (i); //the end of the loop, the game is over    }}

The Java implementation of the Joseph question (drawing on someone else's code + your own analysis of the understanding)

Related Article

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.