Joseph question Java

Source: Internet
Author: User

Joseph Ring: Known n individuals (denoted by number 1,2,3...N respectively) sit around a round table. From the number of people who numbered K, the man who counted to M is out of the way, and his next man counts off from 1, and the person counting to M is out of the way; Repeat until all the people around the table are out.

Knowledge Supplement:

The list interface has two main implementation classes, ArrayList and LinkedList classes. Duplicate elements are allowed in the list collection, unlike set sets, where the elements in the list collection are ordered, and the elements in the list collection can be retrieved based on the index location, the first element to be added is indexed as 0, the second is 1, and so on

Importjava.util.ArrayList; Importjava.util.List; ImportJava.util.Scanner;  Public classtest{ Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); System.out.print ("Please enter the total number of people:"); intTotalnum =Scanner.nextint (); System.out.print ("Please enter the size of the count:"); intCyclenum =Scanner.nextint ();                              Yuesefu (Totalnum, cyclenum);                                                                                                                          }  Public Static voidYuesefu (intTotalnum,intcountnum) {           //number of people initializedlist<integer> start =NewArraylist<integer>();//Create a list collection, and the collection holds an integral type

The correct way to create 13 numbers is as follows:

//list<integer> list=new arraylist<integer> (new Integer);            for(inti = 1; I <= totalnum; i++) {start.add (i); Add elements to the collection like}//counting starting from the first K        intK = 0;  while(Start.size () >0) {k= k +Countnum; //Count toThe index position of the person mk = k% (Start.size ())-1; //System.out.println (Start.size ()); //Judging whether to the end of the team            if(K < 0) {System.out.println (Start.get (start.size ()-1)); System.out.println (3333); Start.remove (Start.size ()-1); K= 0; } Else{System.out.println (Start.get (k));//Get the corresponding value by index   Start.remove (k);                                                         Delete the element at the specified location}}                                                             }                                                         } 

Joseph question Java

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.