Josephu problem

Joseph u problem is: set the number to 1, 2 N people of N sit around for a circle, and the number of K (1<=k<=n) people is agreed to count from 1

, the one who counts to m will be listed, and the next one will report from 1. The one who counts to m will be listed again,

And so on, until all the people are listed, resulting in a sequence of queue numbers.

Tip: use a circular list without leading nodes to handle the Joseph problem:

First, a single cycle list with n nodes is constructed, and then counting from 1 to m from k nodes

The corresponding node is deleted from the list, and then the next node of the deleted node is counted from 1,

Until the last node is deleted from the list.

code implementation

package com.atguigu.linkedList;

public class Josepfu {

public static void main(String[] args) {

//Test traversal and creation methods

CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();

circleSingleLinkedList.addBoy(5);

circleSingleLinkedList.showBoy();

//Test circles circleSingleLinkedList.countBoy(1, 2, 5); }

}

//Create a circular single linked list

class CircleSingleLinkedList{

//Create a first node, currently there is no number

private Boy first=null;

//Add nodes and build a circular list

public void addBoy(int nums) {

//A simple data check by nums

if(nums<1) {

System.out.println("Nums has an incorrect value");

return;

}

Boy curBoy=null;

for(int i=1;i<=nums;i++) {

Boy boy=new Boy(i);

//If it's the first kid

if(i==1) {

first=boy;

first.setNext(first);

curBoy=first;

}else {

curBoy.setNext(boy);

boy.setNext(first);

curBoy=boy;

}

}

}

//Traverse the current ring list public void showBoy() { //Judge whether the list is empty if(first==null) { System.out.println("Empty table"); return; } //Because first can't move, we still use one //Auxiliary pointer to complete traversal Boy curBoy=first; while(true) { System.out.printf("The number of the child is:%d\n",curBoy.getNo()); if(curBoy.getNext()==first) { break;//The description has been traversed } curBoy=curBoy.getNext(); } } //According to the user's input, calculate the order of circles /*startNo:It means counting from the first child * countNum How many times * nums How many children were in the circle at first * * */ public void countBoy(int startNo,int countNum,int nums){ //Verify the data first if(first==null||startNo<1||startNo>nums) { System.out.println("There is a problem with parameter input. Please input again"); return; } //Create auxiliary variables to help children out of circles Boy helper=first; //helper, should point to the last node of the circular list in advance while(true) { if(helper.getNext()==first) { break; } helper=helper.getNext(); } //Let first and helper move startNo-1 times (that is to say, move to the kid who reports the number) before the kid reports the number for(int j=0;j<startNo-1;j++) { helper=helper.getNext(); first=first.getNext(); } //When the child reports, let first, helper move countNum-1 times //Then get ready to go out of the circle. It's a repetitive process until there's only one person in the circle while(true) { if(helper==first) { break; } for(int j=0;j<countNum-1;j++) { helper=helper.getNext(); first=first.getNext(); } //The first node is the node that needs to be circled System.out.printf("Child%d Circle out\n",first.getNo()); first=first.getNext(); helper.setNext(first); } System.out.printf("The number of the last remaining children in the circle is%d\n",first.getNo()); }

}

//Create a Boy class to represent a node

class Boy{

private int no; / / No

private Boy next; / / points to the next node

public Boy() {}

public Boy(int no) {

this.no=no;

}

public int getNo() {

return no;

}

public void setNo(int no) {

this.no = no;

}

public Boy getNext() {

return next;

}

public void setNext(Boy next) {

this.next = next;

}

}

Operation result

It is intuitionistic and easy to understand to solve Joseph's problem with the idea of circular chain list. I hope this article can help you.