约瑟夫环问题(单向循环链表实现)

  • Post author:
  • Post category:其他




1约瑟夫环问题:

​ 已知n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围。约定从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。由此产生一个出队编号序列。

eg:

n=5, 即有5个人

k=1, 从第一个开始报数 ———> (单向环形链表实现)

m=2, 数2下

​ 出队序列: 2、4、1、5、3

构建一个单向环形链表思路

1、先创建第一个节点,让first指向该节点,并形成环形。

2、后面当我们每创建一个新的节点,就该把节点,加入到已有的环形链表中即可。

遍历环形链表

1、先让一个辅助变量(currboy),指向first节点。

2、然后通过一个while循环遍历该环形链表即可,curboy.next = first 结束

public class Josepfu {

	public static void main(String[] args) {
		//测试,环形链表构建和遍历
		 CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
		 circleSingleLinkedList.addBoy(5);//加入五个节点
		 circleSingleLinkedList.showBoy();

	}

}

// 创建一个环形的单向链表
class CircleSingleLinkedList {
	// 创建first节点,当前没有编号
	private Boy first = new Boy(-1);

	// 添加小孩节点构成一个环形链表
	public void addBoy(int nums) {
		// nums 做一个数据校验
		if (nums < 1) {
			System.out.println("nums不正确");
			return;
		}
		Boy curBoy = null;// 辅助变量,帮助构建环形链表
		// 使用for循环创建循环链表
		for (int i = 1; i <= nums; 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 ;
			}
		}
	}
	//遍历当前环形链表
	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();
		}
	}
}

// 创建一个Boy类,表示一个节点
class Boy {
	private int no; // 编号
	private Boy next;// 指向下一个节点,默认null

	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;
	}

}

根据用户输入,生成一个小孩出圈顺序

n=5, 即有5个人

k=1, 从第一个开始报数

m=2, 数2下

public void countBoy(int startNo, int countNum, int nums) {
		// 先对数据校验
		if (first == null || startNo < 1 || startNo > nums) {
			System.out.println("参数输入有误,重新输入");
			return;
		}
		// 创建辅助变量,帮助小孩出圈
		Boy helper = first;
		//
		while (true) {
			if (helper.getNext() == first) {// 说明helper指向最后一个小孩节点
				break;
			}
			helper = helper.getNext();
		}

[外链图片转存失败(img-ZnQLjsyH-1569167255729)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1569160471656.png)]

1、需求创建一个辅助变量helper,事先应该指向环形链表的最后这个节点。

补充:小孩报数前,先让first和helper 移动k-1次

// 小孩报数前,先让当小孩报数时,让first和helper变量同时移动m-1次
		for (int j = 0; j < startNo; j++) {
			helper = helper.getNext();
			first = first.getNext();
		}

2、当小孩报数时,让first和helper变量同时移动m-1次

// 让当小孩报数时,让first和helper变量同时移动countNum-1次,然后出圈
			for (int j = 0; j < countNum; j++) {
				helper = helper.getNext();
				first = first.getNext();
			}

[外链图片转存失败(img-Ei7tTZ6o-1569167255731)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1569160278522.png)]

3、这时就可以让first指向的小孩节点出圈

first = first.next

helper.next =first

[外链图片转存失败(img-PRzK4vlB-1569167255732)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1569160891555.png)]

原来first指向的节点就没有任何引用,就会被回收。

最终代码:

public class Josepfu {

	public static void main(String[] args) {
		// 测试,环形链表构建和遍历
		CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
		circleSingleLinkedList.addBoy(5);// 加入五个节点
		circleSingleLinkedList.showBoy();
		
		//测试小孩出圈是否正确
		circleSingleLinkedList.countBoy(10, 20, 125);

	}

}

// 创建一个环形的单向链表
class CircleSingleLinkedList {
	// 创建first节点,当前没有编号
	private Boy first = new Boy(-1);

	// 添加小孩节点构成一个环形链表
	public void addBoy(int nums) {
		// nums 做一个数据校验
		if (nums < 1) {
			System.out.println("nums不正确");
			return;
		}
		Boy curBoy = null;// 辅助变量,帮助构建环形链表
		// 使用for循环创建循环链表
		for (int i = 1; i <= nums; 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;
			}
		}
	}

	// 遍历当前环形链表
	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();
		}
	}

	// 根据用户的输入,计算出小孩出圈的顺序
	/**
	 * 
	 * @param startNo
	 *            表示从第几个小孩开始数数
	 * @param countNum
	 *            表示数几下
	 * @param nums
	 *            表示最初由多少小孩在圈中
	 */
	public void countBoy(int startNo, int countNum, int nums) {
		// 先对数据校验
		if (first == null || startNo < 1 || startNo > nums) {
			System.out.println("参数输入有误,重新输入");
			return;
		}
		// 创建辅助变量,帮助小孩出圈
		Boy helper = first;
		//
		while (true) {
			if (helper.getNext() == first) {// 说明helper指向最后一个小孩节点
				break;
			}
			helper = helper.getNext();
		}
		// 小孩报数前,先让当小孩报数时,让first和helper变量同时移动m-1次
		for (int j = 0; j < startNo; j++) {
			helper = helper.getNext();
			first = first.getNext();
		}
		// 当小孩报数时,让first和helper变量同时移动m-1次,然后出圈
		// 循环操作,直到只剩一个
		while (true) {
			if (helper == first) {// 只剩下一个人
				break;

			}
			// 让当小孩报数时,让first和helper变量同时移动countNum-1次,然后出圈
			for (int j = 0; j < countNum; j++) {
				helper = helper.getNext();
				first = first.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;// 指向下一个节点,默认null

	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;
	}

}



版权声明:本文为refuse_debug原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。