A question of Joseph ring
The question begins: The Romans captured Chotapat, and 41 of them hid in a cave to escape the catastrophe. These 41 people, including historians Josephus (Joseph) and a friend of the special. The remaining 39 people decided to commit suicide in order to show no submission to the Romans. Everyone decided on a suicide plan, all of these 41 people into a circle, by the first person began to count clockwise, each count of 3 of the person immediately committed suicide, and then again from the next restart Count, is still a count of 3 people immediately on suicide, .... , until all the people were killed in the position. Joseph and his friends did not want to commit suicide, so Joseph thought of a plan, both of them involved in the suicide program, but eventually escaped suicide. Excuse me, how did they do that? First analyze Joseph and his friends want to avoid suicide, then suicide to the mouth pot left three people, and Joseph and his friends should be located in 1 and 2 of the position, so that can avoid suicide, first 41 people into a ring, the inner ring by the first number of clockwise, the outer ring for each number to 3 order, is the Joseph Ring, such as
Here is the code implementation of this problem, (Java)
1 Package Neuq.chao;
2 3 Public classJosephus {4 Static Final intNum = 41;5 Static Final intKillman =3;6 Static voidJosephusintalive) {7 //Alive is the number of people to survive8 intMan[] =New int[Num];//declares an array9 intpose =-1;//PointersTen inti = 0; One intCount = 1; A while(count<=Num) { - Do{ -Pose = (pose+1)%Num; the if(man[pose]==0) {//equals 0, no suicide . -i++; - if(I==killman) {//the number to 3 killed himself. -I=0; + Break; - } + } A} while(true); atMan[pose] =count; -System.out.printf ("The%2d Suicide man Joseph Ring:%2d", man[pose],pose+1); - if(count%2==1){ -System.out.printf ("--"); - } - Else{ inSystem.out.printf ("->\n");//Control output Format - } tocount++;//suicide plus 1. + } -SYSTEM.OUT.PRINTLN ("/n"); theSystem.out.printf ("The initial sequence number of this%d person who needs to survive should be listed in the following sequence number: \ n", alive); *Alive = Num-Alive; $ for(i=0;i<num;i++){Panax Notoginseng if(man[i]>alive) {//the last two people who committed suicide survived. -System.out.printf ("Initial number:%d", i+1); the } + } ASystem.out.printf ("\ n"); the } + Public Static voidMain (String args[]) { -Josephus (2);//Demo $ } $}
To the above question we can be attributed to Joseph Ring (Josephson question) is a mathematical application problem: known n individuals (denoted by number 1,2,3...N respectively) sit around a round table. From the person numbered K began to count, the number to m of the person out of the man, and his next person from 1 began to count, the number to m of the person again, according to this rule repeat until the round table all the people out.
As long as the above code is slightly modified to form a unified solution to this problem
A question of Joseph ring