单向环形链表解决Josephu问题

Josephu问题

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

代码

public class Josephu {
    public static void main(String[] args) {
        CircleSingleLinkedList list = new CircleSingleLinkedList();
        list.addBoy(10);
        list.show();
        list.countBoy(2,5);
    }
}

//单向环形链表
class CircleSingleLinkedList{
    private Boy first = null;

    //向环形链表添加元素
    public void addBoy(int num){
        Boy currentBoy = null;
        for (int i = 1; i <= num; i++) {
            Boy boy = new Boy(i);
            if(i == 1){
                first = boy;
                boy.setNext(first);
                currentBoy = boy;
            }else {
                currentBoy.setNext(boy);
                currentBoy = boy;
                currentBoy.setNext(first);
            }
        }
    }

    //展示环形链表
    public void show(){
        if(first == null){
            System.out.println("链表为空");
            return;
        }

        Boy currentBoy = first;
        while(true){
            System.out.println(currentBoy);
           if(currentBoy.getNext() == first){
               return;
           } else {
               currentBoy = currentBoy.getNext();
           }
        }
    }

    //根据用户的输入,计算出小孩出圈的顺序

    /**
     *
     * @param startNo 表示从哪个开始
     * @param countNum 表示数几下
     */
    public void countBoy(int startNo,int countNum){
        if(first == null || countNum < 1 ){
            System.out.println("参数有误");
            return;
        }
        Boy helper = first;
        while(true){
            if(helper.getNext() == first){
                break;
            }
            helper = helper.getNext();
        }

        for (int i = 0; i < startNo-1; i++) {
            first = first.getNext();
            helper = helper.getNext();
        }

        while(true){
            if(helper == first){
                System.out.println("last"+helper);
                break;
            }
            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;
    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 +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值