Joseph's problem is a famous one: N people are in a circle, starting from the first to report the number, m will be killed, and the last one will be killed. For example, n = 6, M = 5, and the number of the person killed is 5, 4, 6, 2, 3. The last part is the 1st.
Public class demo4 {<br/>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> cyclink = new cyclink (); <br/> cyclink. setlen( 100); <br/> cyclink. creatlink (); <br/> cyclink. setk (2); <br/> cyclink. setm (2); <br/> cyclink. show (); <br/> cyclink. play (); <br/>}< br/> class child {<br/> int no; <br/> child nextchilechild = nu Ll; <br/> public child (INT no) {</P> <p> This. no = no; <br/>}</P> <p >}< br/> // circular linked list <br/> class cyclink {</P> <p> // define the reference pointing to the first child in the linked list <br/> // pointing to the reference of the first child, inactive <br/> child firstchild = NULL; <br/> child temp = NULL; <br/> int Len = 0; // indicates the total number of children <br/> int K; <br/> int m; <br/> // set the linked list size <br/> Public void setlen (INT Len) {<br/> This. len = Len; <br/>}< br/> Public void setk (int K) {<br/> // set the number of people to start counting <br/> This. k = K; <br/>}< br/> Pu BLIC void setm (INT m) {<br/> // set m <br/> This. M = m; <br/>}< br/> Public void play () {<br/> // 1 locate the start Number of Persons </P> <p> child temp = This. firstchild; <br/> for (INT I = 1; I <K; I ++) {<br/> temp = temp. nextchilechild; </P> <p >}< br/> // number of M <br/> while (this. len! = 1) {<br/> for (Int J = 1; j <m; j ++) {</P> <p> temp = temp. nextchilechild; <br/>}</P> <p> child temp2 = temp; // find the first child to be circled <br/> while (temp2.nextchilechild! = Temp) {<br/> temp2 = temp2.nextchilechild; </P> <p >}< br/> // when the number of lectures reaches m, the Child exits. <br/> temp2.nextchilechild = temp. nextchilechild; <br/> temp = temp. nextchilechild; <br/> system. out. println ("out of the loop now" + temp. no); <br/> This. len --; <br/>}< br/> // print the best child <br/> system. out. print ("the last hop is:" + temp. no); <br/>}< br/> // initialize the linked list <br/> Public void creatlink () {<br/> for (INT I = 1; I <= Len; I ++) {</P> <p> if (I = 1) {<br/> // create the first child <br/> chil D child = new child (I); <br/> This. firstchild = Child; <br/> This. temp = Child; <br/>}else <br/>{< br/> if (I = Len) {<br/> child = new child (I ); <br/> temp. nextchilechild = Child; <br/> temp. nextchilechild = This. firstchild; </P> <p>} else {</P> <p> // create a child <br/> child = new child (I ); <br/> temp. nextchilechild = Child; <br/> temp = Child ;} <br/>}</P> <p >}< br/> Public void show () {</P> <p> Chi LD temchild = This. firstchild; <br/> do {<br/> system. out. println (temchild. no + "###"); <br/> temchild = temchild. nextchilechild; <br/>} while (temchild! = This. firstchild); <br/>}< br/>}