Josephu(约瑟夫)问题

本文介绍了Josephu问题的详细描述,包括利用循环链表解决该问题的思路图解和具体实现过程。通过创建一个包含n个节点的单循环链表,从编号为k的节点开始,每数到m的节点就会被删除,直至所有节点出列。
摘要由CSDN通过智能技术生成

问题描述:

设编号为1,2,3…n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m的那个人出列,它的下一位又从1开始报数,数到m的那个人又出列,以此类推,直到所有人出列为止,由此产生一个出队编号的序列.

提示

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

Josephu问题-创建循环链表的思路图解

在这里插入图片描述

Josephu问题-小孩出圈思路图解

在这里插入图片描述

代码实现

package com.itguigu.linkedlist;

public class Josephu {
    public static void main(String[] args) {
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(5);
        circleSingleLinkedList.showBoy();
        System.out.println("小孩的出圈顺序:");
        circleSingleLinkedList.countBoy(1,2,5);
    }
}

/**
 * 创建一个环形单向链表
 * 思路:
 * 1.先创建一个节点,并让first指向该节点,形成环形
 * 2.后面我们每创建一个节点,就将该节点加入到环形链表中
 */
class CircleSingleLinkedList {
    // 创建一个first节点,当前节点没有编号
    private Boy first = null;

    /**
     * 添加小孩节点,构成环形链表
     *
     * @param nums
     */
    public void addBoy(int nums) {
        // 对nums进行校验
        if (nums < 1) {
            System.out.println("输入的nums值不正确");
            return;
        }
        // 创建辅助指针,帮忙构建环形链表
        Boy curBoy = null;
        for (int i = 1; i <= nums; i++) {
            // 1.先创建一个节点,并让first指向该节点,形成环形
            Boy boy = new Boy(i);
            if (i == 1) {
                first = boy;
                first.setNext(first);
                // 辅助指针指向当前boy
                curBoy = boy;
            } else {
                // 2.后面我们每创建一个节点,就将该节点加入到环形链表中
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }
        }
    }

    /**
     * 遍历打印环形链表
     */
    public void showBoy() {
        // 校验环形链表
        if (first == null) {
            System.out.println("链表为空");
            return;
        }
        Boy curBoy = first;
        while (true) {
            System.out.printf("小孩的编号 %d \n", curBoy.getNo());
            if (curBoy.getNext() == first) {
                break;
            }
            // curBoy后移
            curBoy = curBoy.getNext();
        }
    }

    /**
     * 根据用户的输入,计算小孩的出圈顺序
     * @param startNo // 表示从第几个小孩开始数数
     * @param countNum // 表示数几下
     * @param nums // 表示圈中原有的小孩数量
     */
    public void countBoy(int startNo,int countNum,int nums){
        // 进行校验
        if (first == null || startNo < 1 || countNum > nums){
            System.out.println("参数输入有误");
            return;
        }
        // 对应辅助指针helper,让helper指向链表的最后
        Boy helper = first;
        while (true){
            if (helper.getNext() == first){
                // helper已经指向链表最后
                break;
            }
            helper = helper.getNext();
        }
        // 小孩在报数前,让helper和first移动(startNo - 1)次
        for (int i = 0; i < startNo - 1; i++) {
            first = first.getNext();
            helper = helper.getNext();
        }
        // 当小孩在报数时,将helper和first移动(countNum - 1)次,然后出圈
        while (true){
            // 圈中只剩一个小孩时,跳出循环
            if (helper == first){
                break;
            }
            for (int i = 0; i < countNum - 1; i++) {
                first = first.getNext();
                helper=helper.getNext();

            }
            System.out.printf("小孩 %d \n",first.getNo());
            // 从链表中移除小孩节点
            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;
    }

    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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值