Problem HistoryEditIt is said that the famous Jewish historian Josephus had the following story: After the Romans occupied Chotapat, 39 Jews and Josephus and his friends hid in a hole, and 39 Jews decided that they would rather die than be caught by the enemy, so decided a way of suicide, 41 people in a circle, from the 1th person began to count, each off to the 3rd person must commit suicide, and then again from the next re-count, until everyone killed himself. However, Josephus and his friends did not want to follow. Where do you stand to avoid being executed at the outset? Josephus to his friends to pretend to obey, he put friends and himself in the 16th and 31st position, so escaped the game of death. [1] The 17th century French mathematician Gaspar in the "number of game questions" to tell a story: 15 believers and 15 non-believers in the deep sea distress, must be half of the people into the sea, the rest of the people can survive, and then think of a way: 30 people in a circle, starting from the first person to count off, Each number to the nineth person throws him into the sea, so that the cycle lasts until only 15 people. Ask how to arrange the law, so that every time into the sea is non-believers. * Problem analysis and algorithm designJoseph's problem is not difficult, but there are many ways to solve it, and there are many variations of the topic. Here is a way to implement it. here I'm using a two-way loop linked list in a Java data structureThe complete code is as follows:
<pre name= "code" class= "Java" >//Joseph Problem Package Com.test;public class Test {public static void main (string[] args) { Cyclink cyclink= New Cyclink (5,2,3); }}class childnode{int num;//numbered Childnode nextchildnode=null;//points to the next node Childnode prechildnode=null; Public childnode (Int. num) {this. Num=num; }}class cyclink{//Ring Chain list//define a reference to the first node of the list, point to the first node of the reference, cannot move Childnode tofirstchildnode=null; Childnode Temp=null; int len=0;//How many nodes int k=0;//from the first few people start counting int m=0;//number m under int n=1;//nth Personal exit//Set list size public cyclink (int len,int K,int m) {this.len=len;//Set the ring list size this.k=k;//Set the number of people starting from the first few this.m=m;//set the number of M creatlink (); ShowLink (); Play (); }//Start Joseph Rule game private void play () {Childnode cursor=this. Tofirstchildnode; 1. First start to find the person who started the number for (int i=1;i<k;i++) {cursor=cursor.nextchildnode; } while (This.len!=1) {//2. m, found the node to delete for (int j=1;j<m;j++) {cursor=cursor.nextchildnode; In fact no one quoted, then will be GC recycling}//3. Will count to M of the node, exit the ring System.out.println ("+this.n+" the number of times out is: "+cursor. Num); cursor.prechildnode.nextchildnode=cursor.nextchildnode;//Delete Operation cursor.nextchildnode.prechildnode= cursor.prechildnode;//Delete operation//Let the cursor point to the next start number of people, this action is very important, otherwise the cursor will not move cursor=cursor.nextchildnode; this.len--;//out a circle of a man, this.n++; }//The last remaining node System.out.println ("The last person sitting on the table is numbered:" +cursor. Num); }//Initialize ring list private void Creatlink () {for (int i=1;i<=len;i++) {if (i==1) {//Create first node Childnode ch=new Childnode (i); This. Tofirstchildnode=ch; Temp=ch; } else{//Create last node if (i==len) {Childnode ch=new childnode (i); Temp.nextchildnode=ch; Ch.prechildnode=temp; Temp=ch; Temp.nextchildnode=this. Tofirstchildnode; This. Tofirstchildnode.prechildnode=ch; } else{//Continue to create node Childnode ch=new childnode (i); Temp.nextchildnode=ch; Ch.prechildnode=temp; Temp=ch; }}}}//print ring list private void ShowLink () {//define a cursor Childnode cursor=this. Tofirstchildnode; do{System.out.println ("Participant number:" +cursor. Num); Cursor=cursor.prechildnode; } while (Cursor!=this. Tofirstchildnode); }}
Operation Result:Number of participants: 1
Number of participants: 5
Number of participants: 4
Number of participants: 3
Number of participants: 2
The 1th time Out person number is: 4
The 2nd time Out person number is: 2
The 3rd time Out person number is: 1
The 4th time Out person number is: 3
Who was still sitting on the table. ID: 5
Joseph Ring Problem Java code Implementation (high efficiency)