Josephu问题为:设编号为1,2,…n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m的那个人出列,它的下一位又从1开始报数,数到m的那个人又出列,依次类推,直到所有人出列为止,由此产生一个出队编号的序列。
约瑟夫问题–创建环形链表思路图解:
核心代码:遍历小孩节点的时候,自己有写另外一种方法,但是老是缺少最后一个节点,仔细揣摩之后,发现当while条件满足时,不会打印最后一个节点
while(curBoy.getNext() != first) {
System.out.printf("小孩的编号是%d\n",curBoy.getNo());
curBoy = curBoy.getNext();
}
//创建一个环形的单向链表
class CircleSingleLinkedList {
// 创建一个first节点,当前无编号
private Boy first = null;
// 添加小孩构建成环形链表
public void addBoy(int num) {
// 数据校验
if (num < 1) {
System.out.println("值不正确");
return;
}
Boy curBoy = null;// 辅助指针,帮助构建环形链表
// 使用for来创建环形链表
for (int i = 1; i <= num; i++) {
// 根据编号,创建小孩节点
Boy boy = new Boy(i);
// 如果是第一个小孩
if (i == 1) {
first = boy;
first.setNext(first);// 构成一个环
curBoy = first;// 让curBoy指向第一个小孩
} else {
curBoy.setNext(boy);// 添加新的小孩到链表里
boy.setNext(first);// 使链表变成一个环
curBoy = boy;// 后移,curBoy是指的是当前添加小孩的上一个小孩,添加好之后就变成了该小孩
}
}
}
// 遍历当前的环形链表
public void showBoy() {
// 判断链表是否为空
if (first == null) {
System.out.println("链表为空");
return;
}
// 因为first不能动,因此使用辅助指针
Boy curBoy = first;
while (true) {
System.out.printf("小孩的编号是%d\n", curBoy.getNo());
if (curBoy.getNext() == first) {
break;
}
curBoy = curBoy.getNext();
}
/*
* while(curBoy.getNext() != first) {
* System.out.printf("小孩的编号是%d\n",curBoy.getNo());
* curBoy = curBoy.getNext(); }
*/
}
约瑟夫问题–小孩出圈思路图解:
代码:注意helper是指向最后一个节点的,first是第一个节点,他们是同时移动的
//将first指向的小孩节点出圈,这里用的是set方法
first = first.getNext();
helper.setNext(first);//这句代码很神奇
// 根据用户的输入,计算出出圈的顺序
/*
* startNo 表示从第几个小孩开始数数
* countNum 表示数几下
* nums 表示有几个小孩在圈中
*/
public void countBoy(int startNo, int countNum, int nums) {
// 对数据进行校验
if (first == null || startNo < 1 || startNo > nums) {
System.out.println("参数输入有误,请重新输");
return;
}
// 创建一个辅助指针,帮助小孩出圈
Boy helper = first;
// 让helper指向最后一个节点
while (true) {
if (helper.getNext() == first) {
break;
}
helper = helper.getNext();
}
//小孩报数前,先让first和helper移动k-1次,因为开始报数的位置不一定是第一个小孩
for(int j= 0;j<startNo -1 ;j++) {
first = first.getNext();
helper = helper.getNext();
}
//当小孩报数时,让first和helper 指针同时的移动m -1 次,然后出圈
//这里是一个循环操作,直到圈中只有一个节点
while(true) {
if(helper == first) {//说明圈中只有一个节点
break;
}
//让first和helper指针同时移动coutNum - 1
for(int j= 0;j < countNum-1;j++) {
first = first.getNext();
helper = helper.getNext();
}
//这时first指向的节点,就是要出圈的小孩节点
System.out.printf("小孩%d出圈\n",first.getNo());
//这时将first指向的小孩节点出圈
first = first.getNext();
helper.setNext(first);//这句代码很神奇
}
System.out.printf("最后留在圈中小孩编号%d\n",first.getNo());
}
}
//创建一个Boy,表示一个节点
class Boy {
private int no;// 编号
private Boy next;// 指向下一个节点
public Boy(int no) {
this.no = no;
}
@Override
public String toString() {
return "Boy [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;
}
}
全部代码:
`
package com.suanfa;
public class Josephu {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 测试构建和测试
CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
circleSingleLinkedList.addBoy(5);// 加入五个小孩节点
//circleSingleLinkedList.showBoy();
circleSingleLinkedList.countBoy(1, 2, 5);
}
}
//创建一个环形的单向链表
class CircleSingleLinkedList {
// 创建一个first节点,当前无编号
private Boy first = null;
// 添加小孩构建成环形链表
public void addBoy(int num) {
// 数据校验
if (num < 1) {
System.out.println("值不正确");
return;
}
Boy curBoy = null;// 辅助指针,帮助构建环形链表
// 使用for来创建环形链表
for (int i = 1; i <= num; i++) {
// 根据编号,创建小孩节点
Boy boy = new Boy(i);
// 如果是第一个小孩
if (i == 1) {
first = boy;
first.setNext(first);// 构成一个环
curBoy = first;// 让curBoy指向第一个小孩
} else {
curBoy.setNext(boy);// 添加新的小孩到链表里
boy.setNext(first);// 使链表变成一个环
curBoy = boy;// 后移,curBoy是指的是当前添加小孩的上一个小孩,添加好之后就变成了该小孩
}
}
}
// 遍历当前的环形链表
public void showBoy() {
// 判断链表是否为空
if (first == null) {
System.out.println("链表为空");
return;
}
// 因为first不能动,因此使用辅助指针
Boy curBoy = first;
while (true) {
System.out.printf("小孩的编号是%d\n", curBoy.getNo());
if (curBoy.getNext() == first) {
break;
}
curBoy = curBoy.getNext();
}
/*
* while(curBoy.getNext() != first) {
* System.out.printf("小孩的编号是%d\n",curBoy.getNo()); curBoy = curBoy.getNext(); }
*/
}
// 根据用户的输入,计算出出圈的顺序
/*
* startNo 表示从第几个小孩开始数数 countNum 表示数几下 nums 表示有几个小孩在圈中
*/
public void countBoy(int startNo, int countNum, int nums) {
// 对数据进行校验
if (first == null || startNo < 1 || startNo > nums) {
System.out.println("参数输入有误,请重新输");
return;
}
// 创建一个辅助指针,帮助小孩出圈
Boy helper = first;
// 让helper指向最后一个节点
while (true) {
if (helper.getNext() == first) {
break;
}
helper = helper.getNext();
}
//小孩报数前,先让first和helper移动k-1次,因为开始报数的位置不一定是第一个小孩
for(int j= 0;j<startNo -1 ;j++) {
first = first.getNext();
helper = helper.getNext();
}
//当小孩报数时,让first和helper 指针同时的移动m -1 次,然后出圈
//这里是一个循环操作,直到圈中只有一个节点
while(true) {
if(helper == first) {//说明圈中只有一个节点
break;
}
//让first和helper指针同时移动coutNum - 1
for(int j= 0;j < countNum-1;j++) {
first = first.getNext();
helper = helper.getNext();
}
//这时first指向的节点,就是要出圈的小孩节点
System.out.printf("小孩%d出圈\n",first.getNo());
//这时将first指向的小孩节点出圈
first = first.getNext();
helper.setNext(first);//
}
System.out.printf("最后留在圈中小孩编号%d\n",first.getNo());
}
}
//创建一个Boy,表示一个节点
class Boy {
private int no;// 编号
private Boy next;// 指向下一个节点
public Boy(int no) {
this.no = no;
}
@Override
public String toString() {
return "Boy [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;
}
}
`