Joseph's question is a famous one:
In a circle of N people, the first one starts to report the number, the first m will be killed, the last one will be killed, and the rest 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.
The Code is as follows:
/*** Joseph question ** @ author yxh **/public class ysf {public void operate (INT nump, int index, int start) {If (nump <start) {system. out. println ("the start number cannot exceed the total number"); return;} If (index <= 0) {system. out. println ("index value must be greater than 0"); return;} linknode Ln = new linknode (); int COUNT = nump; // record // first generate a linked list based on the given number of people for (INT I = 1; I <= nump; I ++) {ln. add (I);} // then count the number of index times starting from the start position, and then stop the node column node = ln. findnode (start); // number of records starting from this node: system. out. prin TLN ("slave" + node. getdata () + "start"); While (true) {for (Int J = 1; j <index; j ++) {If (node = NULL) {node = ln. gethead ();} else {node = node. getnextnode (); If (node = NULL) node = ln. gethead () ;}} if (node! = NULL) {system. out. println ("delete:" + node. getdata (); ln. deletenode (node. getdata (); ln. printnode (); count --; If (COUNT = 0) {ln. printnode (); If (node! = NULL) system. out. println ("the number of Joseph is:" + node. getdata (); break;} node = node. getnextnode (); If (node = NULL) node = ln. gethead (); system. out. println ("slave:" + node. getdata () + "") ;}} public static void main (string [] ARGs) {ysf YF = new ysf (); YF. operate (6, 5, 1 );}}
Linknode class:
/*** Create a linked list * to implement Joseph's problem */class linknode {private node head; // head node public node gethead () {return head ;} public void sethead (node head) {This. head = head;} public void add (INT data) {node = new node (data); If (Head = NULL) {head = node;} else {head. addnode (node) ;}} public node findnode (INT data) {If (Head = NULL) {return NULL;} else {return head. find (data) ;}} public void deletenode (INT data) {If (Head! = NULL) {If (head. getdata () = data) {head = head. getnextnode ();} else {If (head. getnextnode ()! = NULL) {head. getnextnode (). Delete (Head, data) ;}}} public void printnode () {If (Head! = NULL) {head. Print ();}}}
Finally, the node class:
Public class node {private int data; // data public int getdata () {return data;} public void setdata (INT data) {This. data = data;} private node nextnode; // The next public node getnextnode () {return nextnode;} public void setnextnode (node nextnode) {This. nextnode = nextnode;} public node (INT data) {This. data = data;} // Add a node to the public void addnode (node) {If (this. nextnode! = NULL) {This. nextnode. addnode (node);} else {This. nextnode = node ;}/// find the public node find (INT data) {If (this. data = data) {return this;} else {If (this. nextnode! = NULL) {return this. nextnode. find (data) ;}} return NULL ;}// Delete the public void Delete (node prenode, int data) {If (this. data = data) {prenode. nextnode = This. nextnode;} else {If (this. nextnode! = NULL) {This. nextnode. delete (this, data) ;}}// output node information public void print () {system. out. print (this. data + "---->"); If (this. nextnode! = NULL) {This. nextnode. Print ();}}}
The following is the result of the test output:
Starting from 1
Delete: 5
1 ----> 2 ----> 3 ----> 4 ----> 6 ----> Start from: 6
Delete: 4
1 ----> 2 ----> 3 ----> 6 ----> Start from: 6
Delete: 6
1 ----> 2 ----> 3 ----> Start from: 1
Delete: 2
1 ----> 3 ----> Start from: 3
Delete: 3
1 ----> Start from: 1
Delete: 1
Joseph number is: 1
The above is your own implementation. If there is anything wrong, I hope you can correct it. Thank you.