The Java implementation of the Joseph problem

Source: Internet
Author: User

The problem of throwing a handkerchief, also known as Joseph. I have nothing to do to try to achieve a bit, the realization of logic is simple, no complex algorithm, suitable for novice reference.

//parameter step refers to stepping value, step to several//parameter count refers to a total of several people     Public Static intDiushoupa (intStepintcount) {                //simulate a queue with listlist<integer> queue =NewArraylist<integer>();  for(inti = 1; I <= count; i++) {queue.add (i); System.out.println ("Count off:" +i); }//Loopindex: The coordinates of the queue in the while loop        intLoopindex=0;  while(true) {            //T: Used to record if there is enough cycle time            intt = 1; //this layer while is used for stepping step             while(t<=Step) {                                //If you loop to the end of the queue, the loop starts at coordinate 1                if(Loopindex > Queue.size ()-1) {Loopindex= 0; }                //If the step is looped, then the item on this coordinate is loopindex to be removed .                if(T = =Step) {System.out.println ("Dequeue:" +queue.get (loopindex));                Queue.remove (loopindex); }Else{                    //only if there is no removal of the item (dequeue) when the loop coordinate is + +, when you remove an item, the object in the collection will be pushed forward, so the coordinates do not need + +loopindex++; } t++; }            //returns the last Dequeue object if the size of the collection is not enough to step at once            if(Queue.size () <Step) {                return Loopindex; }        }

The Java implementation of the Joseph problem

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.