1. 程式人生 > 實用技巧 >演算法學習第四日之單向環形連結串列解決Josephu問題

演算法學習第四日之單向環形連結串列解決Josephu問題

約瑟夫問題



Josephu問題的程式碼實現:

package com.atguigu.linkedlist;

public class Josepfu {

	public static void main(String[] args) {
		// 測試一把看看構建環形連結串列,和遍歷是否ok
		CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
		circleSingleLinkedList.addBoy(125);// 加入5個小孩節點
		circleSingleLinkedList.showBoy();
		
		//測試一把小孩出圈是否正確
		circleSingleLinkedList.countBoy(10, 20, 125); // 2->4->1->5->3
		//String str = "7*2*2-5+1-5+3-3";
	}

}

// 建立一個環形的單向連結串列
class CircleSingleLinkedList {
	// 建立一個first節點,當前沒有編號
	private Boy first = null;

	// 新增小孩節點,構建成一個環形的連結串列
	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(); // curBoy後移
		}
	}

	// 根據使用者的輸入,計算出小孩出圈的順序
	/**
	 * 
	 * @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;
		// 需求建立一個輔助指標(變數) helper , 事先應該指向環形連結串列的最後這個節點
		while (true) {
			if (helper.getNext() == first) { // 說明helper指向最後小孩節點
				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 指標同時 的移動 countNum - 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; // 指向下一個節點,預設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;
	}

}