约瑟夫环问题

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

建立男孩表

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.atguigu.linkedlist;

class Boy {
    private int no;
    private Boy next;

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

    public int getNo() {
        return this.no;
    }

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

    public Boy getNext() {
        return this.next;
    }

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

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.atguigu.linkedlist;

class CircleSingleLinkedList {
    private Boy first = null;

    CircleSingleLinkedList() {
    }
	
    public void addBoy(int nums) {
        if (nums < 1) {
            System.out.println("nums的值不正确");
        } else {
            Boy curBoy = null;

            for(int i = 1; i <= nums; ++i) {
                Boy boy = new Boy(i);
                if (i == 1) {
                    this.first = boy;
                    this.first.setNext(this.first);
                    curBoy = this.first;
                } else {
                    curBoy.setNext(boy);
                    boy.setNext(this.first);
                    curBoy = boy;
                }
            }

        }
    }

    public void showBoy() {
        if (this.first == null) {
            System.out.println("没有任何小孩~~");
        } else {
            Boy curBoy = this.first;

            while(true) {
                System.out.printf("小孩的编号 %d \n", curBoy.getNo());
                if (curBoy.getNext() == this.first) {
                    return;
                }

                curBoy = curBoy.getNext();
            }
        }
    }

    /**
     *
     * @param startNo 表示从第几个小孩开始数数
     * @param countNum 表示数几下
     * @param nums 表示最初有多少小孩在圈中
     */
    public void countBoy(int startNo, int countNum, int nums) {
        if (this.first != null && startNo >= 1 && startNo <= 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();
                }
                System.out.printf("小孩%d出圈\n", this.first.getNo());
                first = first.getNext();
                helper.setNext(first);
            }


            System.out.printf("最后留在圈中的小孩编号%d \n", this.first.getNo());
        } else {
            System.out.println("参数输入有误, 请重新输入");
        }
    }
}

测试代码

package com.atguigu.linkedlist;

public class Josepfu {
    public Josepfu() {
    }

    public static void main(String[] args) {
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(25);
        circleSingleLinkedList.showBoy();
        circleSingleLinkedList.countBoy(1, 2, 25);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值