链表_单向环形链表(约瑟夫问题)

约瑟夫问题

  • 1,2,3,4… n个人围成一圈,从k开始报数,第m个人数列,循环,直到所有人出列。产生一个出队编号
/**
 * 约瑟夫问题
 * 1,2,3,4... n个人围成一圈,从k开始报数,第m个人数列,循环,直到所有人出列。产生一个出队编号
 */
public class Josephus {

    public static void main(String[] args) {
        //构建环形
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.add(5);
        //遍历
        circleSingleLinkedList.show();

        //出圈
        circleSingleLinkedList.countBoy(1,2);
    }

}

/**
 * 环形,单向,链表
 */
class CircleSingleLinkedList {

    //第一个节点
    private Boy first;

    //创建环形单链表
    public void add(int nums) {
        if (nums < 1) {
            return;
        }
        //辅助指针
        Boy curBoy = null;

        for (int i = 1; i <= nums; i++) {
            //创建节点
            Boy boy = new Boy(i);

            //第一个节点
            if (boy.getNo() == 1) {
                first = boy;
                //构成环状
                first.setNext(first);
                //当前指针指向
                curBoy = first;
            } else {
                //当前节点下一指针指向
                curBoy.setNext(boy);
                //移动当前节点
                curBoy = boy;
                //组成环形
                curBoy.setNext(first);
            }


        }

    }

    //    显示
    public void show() {
        if (first == null) {
            return;
        }
        Boy curBoy = first;
        while (true) {

            System.out.println(curBoy);

            if (curBoy.getNext() == first) {
                break;
            }

            curBoy = curBoy.getNext();

        }
    }


    /**
     * 根据输入,计算出圈的顺序
     *
     * @param startNo  第 startNo 个节点开始数
     * @param countNum 数 countNum 次
     */
    public void countBoy(int startNo, int countNum) {
//校验
        if (startNo < 1 ) {
            System.out.println("参数输入有误");
            return;
        }
        //辅助指针
        Boy helper = first;

        // ①helper指向最后一个节点
        while (true) {
            if (helper.getNext() == first) {
                break;
            }
            helper = helper.getNext();
        }

        // ② first和helper 移动 startNo-1
        for (int i = 0; i < startNo - 1; i++) {
            first = first.getNext();
            helper = helper.getNext();
        }

        // ③出圈
        while (true) {
            if (helper == first) {
                //只剩一个节点
                System.out.println("出圈节点:" + first);
                break;
            }

//            first和helper 移动 countNum-1 次
            for (int i = 0; i < countNum - 1; i++) {
                first = first.getNext();
                helper = helper.getNext();
            }
            System.out.println("出圈节点:" + first);
            // 出圈
            first = first.getNext();
            helper.setNext(first);
        }


    }
}


/**
 * 表示一个节点
 */
class Boy {
    //编号
    private int no;
    //next
    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;
    }

    @Override
    public String toString() {
        return "Boy{" +
                "no=" + no +
                '}';
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值