学习笔记-约瑟夫问题之环形链表

约瑟夫问题java代码

curBoy为辅助节点

我测试的是5个人的,最终答案是24153。

public class Josepfu {
    public static void main(String[] args) {
        CircleSingleLinkedList cs = new CircleSingleLinkedList();
        cs.addBoy(5);
        cs.list();
        //测试小孩出圈是否成功
        cs.countBoy(1, 2, 5);
    }
}

class CircleSingleLinkedList{
    private Boy first = null;

    public void addBoy(Integer nums){
        if (nums < 1){
            System.out.println("编号不正确!");
            return;
        }
        Boy curBoy = null;
        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 list(){
        //判断是否为空
        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 countNums 表示数几下
     * @param nums 表示最初有多少小孩在圈中
     */
    public void countBoy(int startNo, int countNums, 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();
        }
        //当小孩报数时,helper和first同时移动m-1次
        while (true){
            //说明圈中只有一个节点
            if (helper == first){
                break;
            }

            //让first和helper同时移动countNum-1
            for (int j = 0; j < countNums - 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", first.getNo());
    }

}

class Boy{
    private Integer no;
    private Boy next;

    public Boy(Integer no){
        this.no = no;
    }

    public Integer getNo() {
        return no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNo(Integer no) {
        this.no = no;
    }

    public void setNext(Boy next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "Boy{" +
                "no=" + no +
                ", next=" + next +
                '}';
    }
}

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

原创文章 23 获赞 23 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Shine_QianMo/article/details/104950262