已知n个小孩以编号1,2,3…n分别表示)围坐在一张圆桌周围。从编号为1的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。
假设:n=5 (有5个人)m=2(每次数两个)k=1(从第k个人开始数)
约瑟夫问题-创建环形链表的思路图解
构建一个单向的环形链表思路
- 先创建第一个节点,让first指向该节点,并形成环形
- 后面当我们每创建一个新节点,就把该节点,加入到已有的环形链表中即可
遍历环形链表
- 先让一个辅助变量curBoy指向first节点
- while循环遍历链表即可。
循环条件curBoy=curBoy.next
终止条件curBoy.next=first
约瑟夫问题-节点出圈思路分析
根据用户的输入,生产一个小孩出圈的顺序
n=5 (有5个人)m=2(每次数两个)k=1(从第k个人开始数)
- 创建一个辅助变量helper,事先应该指向环形链表的最后这个节点
- 小孩报数前,因为first在第一个节点,helper在first前,从第k个人开始数,所以先让first与helper移动k-1次
- 当小孩报数时,让first与helper移动m-1次
- 这时将first指向的节点出圈
first=first.next;
helper.next=first
出圈顺序
2->4->1->5->3
约瑟夫问题-代码实现
/**
* @ClassName: Josepfu
* @Description: problem about Josepfu
* @author author
* @date 2020-08-10
*/
public class Josepfu {
public static void main(String[] args) {
CirleSingleLinkedList cirleSingleLinkedList = new CirleSingleLinkedList();
cirleSingleLinkedList.addBoy(5);
cirleSingleLinkedList.showBoy();
cirleSingleLinkedList.countBoy(1, 2, 5);
}
}
/**
* @ClassName: CirleSingleLinkedList
* @Description: construct a CirleSingleLinkedList
* @author author
* @date 2020-08-10
*/
class CirleSingleLinkedList {
// 创建first节点,当前没有编号
private Boy first = null;
/**
* @Title: addBoy
* @Description: add boyNode,construct a CirleSingleLinkedList
* @param nums = boyNode's numbers
*/
public void addBoy(int nums) {
if (nums < 1) {
System.out.println("nums的值不正确。");
return;
}
Boy curBoy = null; // 辅助指针
// for循环添加节点,至少要有一个所以i=1
for (int i = 1; i <= nums; i++) {
Boy boy = new Boy(i);
if (i == 1) {
first = boy;
first.setNext(first); // 构成环
curBoy = first; // 辅助变量指向第一个节点
} else {
curBoy.setNext(boy); // 添加节点
boy.setNext(first);// 构成环
curBoy = boy;// 辅助节点后移
}
}
}
/**
* @Title: showBoy
* @Description: ergotic list(遍历链表)
*/
public void showBoy() {
// 判断是否为空
if (first == null) {
System.out.println("链表为空。");
return;
}
Boy curBoy = first; // 创建辅助变量
while (curBoy.getNext() != first) {
// 循环结束条件为辅助节点下一个为first节点
System.out.printf("小孩的编号 %d\n", curBoy.getNo());
// 辅助节点前移,完成遍历
curBoy = curBoy.getNext();
}
}
/**
* @Title: countBoy
* @Description: countBoy
* @param startNo: position of start number
* @param countNo: how many times need to count
* @param nums: children's number
*/
public void countBoy(int startNo, int countNo, int nums) {
// verification data(校验数据)
if (first == null || startNo < 1 || countNo > nums) {
System.out.println("参数输入有误,重新输入");
}
// create a temp variable
Boy helper = first;
while (true) {
if (helper.getNext() == first) {
break;
}
// 使用.getNext原因是 next是private私有变量,所以要用.get方法来用next
helper = helper.getNext();
}
//报数前先让小孩移动k-1次;
for (int i = 0; i < startNo - 1; i++) {
first = first.getNext();
helper = helper.getNext();
}
// 当小孩报数时,helper和first移动countNo-1次,然后出圈
while (true) {
if (helper == first) {// 循环结束
break;
}
for (int i = 0; i < countNo - 1; i++) {
first = first.getNext(); // first是目标boy
helper = helper.getNext();
}
System.out.printf("出圈小孩编号%d\n", first.getNo());
first = first.getNext();
helper.setNext(first);
}
System.out.printf("最后留在圈中的小孩%d\n", helper.getNo());
}
}
/**
* @ClassName: Boy
* @Description: class about Boy
* @author author
* @date 2020-08-10
*/
class Boy {
private int no; // number
private Boy next; // point to the next one
/**
* @Title: Boy
* @Description: Boy构造函数
* @param no
* @author author
* @date 2020-08-10 06:11:28
*/
public Boy(int no) {
this.no = no;
}
/**
* @Title: getNo
* @Description: ctrl+shift+s 快捷调用get set方法
* @return
*/
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;
}
}
Tips:
- eclipse方法注释快捷键:alt+shift+j
- eclipse代码格式化快捷键:ctrl+shift+f
- ctrl+shift+s 快捷调用get set方法