数据结构与算法-约瑟夫问题 Josephu问题


在这里插入图片描述

Josephu 问题

(约瑟夫、约瑟夫环)
Josephu 问题为:设编号为1,2,… n 的 n 个人围坐一圈,约定编号为 k (1<= k <= n )的人从1开始报数,数到 m 的那个人出列,它的下一位又从1开始报数,数到 m 的那个人又出列,依次类推,直到所有人出列为止,由此产生一个出队编号的序列。

提示:用一个不带头结点的循环链表来处理 Josephu 问题:先构成一个有个结点的单循环链表,然后由 k 结点起从1开始计数,计到 m 时,对应结点从链表中删除,然后再从被删除结点的下一个结点又从1开始计数,直到最后一个结点从链表中删除算法结束。

小案例

假设n = 5, k =1, m =2
出队编号的顺序: 2-》4-》1-》5-》3
可以自行画图做一个验证

思路分析

在这里插入图片描述

代码实现

package com.chad.lambda;

public class JosephoLinkedList {
    public static void main(String[] args) {
        //测试构建链表和遍历是否正确
        circleLinkedList circleLinkedList = new circleLinkedList();
        circleLinkedList.addBoy(5);
        circleLinkedList.showBoy();

        circleLinkedList.countBoy(1,2,5);

    }
}

class circleLinkedList{
    private Boy first = new Boy(-1);

    //添加小孩构建一个环形链表
    void addBoy(int 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.next = first;
                first.setNext(first);
                curBoy = first;
            }else{
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }
        }

    }

    //遍历当前环形链表
    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){
                System.out.println("已经遍历完毕了");
                break;
            }
            curBoy = curBoy.getNext();

        }
    }

    /**
     *
     * @param startNum 表示第几个小孩开始数
     * @param countNum 数几下
     * @param nums 表示最初有多少个小孩在圈里
     */
    void countBoy(int startNum,int countNum, int nums){
        if(first == null || startNum < 1 || startNum > nums){
            System.out.println("参数输入有误");
            return;
        }
        //创建一个辅助指针,帮助完成小孩出圈
        Boy helper = first;
        while (true){
            if(helper.getNext() == first){
                System.out.println("指向第一个节点了");
                break;
            }
            helper = helper.getNext();
        }
        for (int j = 0; j < startNum -1; j++) {
            first = first.getNext();
            helper = helper.getNext();
        }
        //循环操作,直到圈中只有一个
        while(true){
            if(helper == first){
                break;
            }
            for (int j = 0; j < countNum - 1; j++) {
                first = first.getNext();
                helper = helper.getNext();
            }
            System.out.printf("小孩%d出圈\n",first.getNo());
            first = first.getNext();
            helper.setNext(first);
        }
        System.out.printf("最后留在圈中的小孩节点的编号是%d",helper.getNo());
    }
}

class Boy{
    private int no;
    public Boy next;

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

运行代码结果
在这里插入图片描述

这样我们的代码没有问题。
在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chad__chang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值