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