/*
* Problem prototypes
*/
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. Joseph arranged his friends in the 16th and 31st positions and escaped the game of death.
/*
* Problem Analysis
*/
This problem is also described as the problem of dropping a handkerchief, so the code is named to Shoujuan
Since I was learning Java recently, I decided to use the Java list to complete the problem. Design a circular linked list, link 41 people, select a person as the starting point, every 3 to take the Remove action.
Design class Required:
Shoujuan, used to denote everyone. Contains a member: number is used to count, and Nextjuan is used to denote the next person.
Shoujuanlist, used to represent a linked list. Include member: Shoujuanfirst is used to denote the first person, SHOUJUANNBR is used to denote the total number of people. Include Method: Makecircle () Creates a ring list, Addshoujuan () adds a node, Removeshoujuan () deletes a node.
/*
* Analysis of difficulties
*/
In the process of completing this code, the main consideration is the characteristic of the cyclic list, that is, the last one points to the head node. The completion of this code needs to have a very clear understanding of the value and the address, the incoming address will affect the value of this location.
A special consideration is required for the head node and the second node to prevent the omission of the case.
The method is not the simplest, as a mark on the way to study, mark.
/*
* Code
*/
Shoujuan.java
1 PackageCom.diushoujuan;2 3 Public classShoujuan {4 Private intNumber ;5 PrivateShoujuan Nextjuan =NULL;6 7 PublicShoujuan (intN) {8 This. Number =N;9 }Ten One Public intGetNumber () { A returnNumber ; - } - the Public voidSetnumber (intNumber ) { - This. Number =Number ; - } - + PublicShoujuan Getnextjuan () { - returnNextjuan; + } A at Public voidSetnextjuan (Shoujuan Nextjuan) { - This. Nextjuan =Nextjuan; - } - - Public voidShowNbr () { -System.out.println ("NUB is:" + This. number); in } -}
Shoujuanlist.java
1 PackageCom.diushoujuan;2 3 /*4 * Create a circular link list5 */6 Public classShoujuanlist {7 PrivateShoujuan Shoujuanfirst =NULL;8 9 Private intSHOUJUANNBR = 0;Ten One Public intGetshoujuannbr () { A returnSHOUJUANNBR; - } - the Public voidSETSHOUJUANNBR (intSHOUJUANNBR) { - This. SHOUJUANNBR =SHOUJUANNBR; - } - + PublicShoujuan Getshoujuanfirst () { - returnShoujuanfirst; + } A at Public voidSetshoujuanfirst (Shoujuan shoujuanfirst) { - This. Shoujuanfirst =Shoujuanfirst; - } - - //Manufacturing ring Chain list - Public voidmakecircle () { in if(Shoujuanfirst! =NULL){ -Shoujuan shoujuantemp =Shoujuanfirst; to while(NULL!=Shoujuantemp.getnextjuan ()) { +Shoujuantemp =Shoujuantemp.getnextjuan (); - } the Shoujuantemp.setnextjuan (shoujuanfirst); * } $ }Panax Notoginseng - //Add a node the Public voidAddshoujuan (Shoujuan Shoujuan) { +Shoujuan shoujuantemp =NULL; A if(Shoujuanfirst = =NULL){ theShoujuanfirst =Shoujuan; +}Else{ -Shoujuantemp =Shoujuanfirst; $ while(NULL!=Shoujuantemp.getnextjuan ()) { $Shoujuantemp =Shoujuantemp.getnextjuan (); - } - Shoujuantemp.setnextjuan (Shoujuan); the } -shoujuannbr++; Wuyi } the - //Delete a node Wu Public voidRemoveshoujuan (Shoujuan Shoujuan) { -Shoujuan shoujuantemp =Shoujuanfirst; About if(Shoujuantemp = = Shoujuanfirst && shoujuantemp.getnumber () = = Shoujuan.getnumber () && SHOUJUANNBR = 1) { $Shoujuanfirst =NULL; -shoujuannbr--; -}Else if(Shoujuantemp = = Shoujuanfirst && shoujuantemp.getnumber () = = Shoujuan.getnumber () && SHOUJUANNBR = 2){ -Shoujuanfirst =Shoujuanfirst.getnextjuan (); Ashoujuannbr--; +}Else if(Shoujuantemp = = Shoujuanfirst && shoujuantemp.getnumber () = = Shoujuan.getnumber () && shoujuannbr>2){ the while(Shoujuantemp.getnextjuan ()! =Shoujuanfirst) { -Shoujuantemp =Shoujuantemp.getnextjuan (); $ } the Shoujuantemp.setnextjuan (Shoujuanfirst.getnextjuan ()); theShoujuanfirst =Shoujuanfirst.getnextjuan (); theshoujuannbr--; the}Else{ - if(Shoujuantemp = = Shoujuanfirst && shoujuantemp.getnumber ()! =Shoujuan.getnumber ()) { inShoujuantemp =Shoujuantemp.getnextjuan (); the } the while(true){ About if(Shoujuantemp.getnextjuan (). GetNumber () = =Shoujuan.getnumber ()) { the Shoujuantemp.setnextjuan (Shoujuantemp.getnextjuan (). Getnextjuan ()); theshoujuannbr--; the Break; + } -Shoujuantemp =Shoujuantemp.getnextjuan (); the } Bayi } the } the}
Playgame.java
PackageCom.diushoujuan; Public classPlayGame { Public Final Static intSHOUJUANNBR = 2; Public Final Static intSHOUJUANREMOVENBR = 2; Public Static voidMain (string[] args) {shoujuanlist shoujuanlist=Newshoujuanlist (); for(inti = 1; i<=shoujuannbr; i++) {Shoujuan Shoujuan=NewShoujuan (i); Shoujuanlist.addshoujuan (Shoujuan); } shoujuanlist.makecircle (); Shoujuan Shoujuanremove=Shoujuanlist.getshoujuanfirst (); while(SHOUJUANLIST.GETSHOUJUANNBR () >=SHOUJUANREMOVENBR) { if(Shoujuanremove! =Shoujuanlist.getshoujuanfirst ()) {Shoujuanremove=Shoujuanremove.getnextjuan (); } for(inti = 1;i< shoujuanremovenbr;i++) {Shoujuanremove=Shoujuanremove.getnextjuan (); } Shoujuanlist.removeshoujuan (Shoujuanremove); } Shoujuan shoujuanshow=Shoujuanlist.getshoujuanfirst (); for(inti = 1;i< shoujuanremovenbr;i++) {SHOUJUANSHOW.SHOWNBR (); Shoujuanshow=Shoujuanshow.getnextjuan (); } }}
A solution to Joseph's problem