約瑟夫問題


Josephu(約瑟夫,約瑟夫環)問題

問題表述為:設編號為1,2,...,n的n個人圍坐一圈,約定編號為K(1<=k<=n)的人開始報數,數到m的那個人出列,它的下一位又從1開始報數,數到m的那個人又出列,依此類推,直到所有人出列為止,由此產生一個出隊編號的序列

 

 (一)約瑟夫問題-創建環形鏈表的思路圖解

 

 (二)約瑟夫問題-小孩出圈的思路分析圖

 

 

package linkedlist;

public class Josepfu {

    public static void main(String[] args) {
        //測試構建環形鏈表與顯示環形鏈表是否正確
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(5);//加入五個節點
        circleSingleLinkedList.showBoy();
        //測試小孩出圈是否正常
        circleSingleLinkedList.countBoy(1, 2, 5);
    }

}
//創建環形的單向鏈表
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;
        }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) {
                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.getNext();
            helper.setNext(first);
        }
        System.out.printf("最后留在圈中的小孩編號為%d",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;
    }
}

運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM