zoukankan      html  css  js  c++  java
  • 单向环形链表解决约瑟夫环(Josephus)问题

    一、约瑟夫环问题

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

    二、解决思路

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

    三、实现代码

    1.需要的数据结构

    节点

    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 +
                    '}';
        }
    }
    

    单向环形链表

    class CircleList{
    
        private Boy first = null;
    
        /**
         *
         * @param startNo   从第几个小孩开始数
         * @param countNo   每次数几下
         * @param nums 小孩的数量
         */
        public void outOfCircle(int startNo, int countNo,int nums){
            if (first==null || startNo<1|| startNo>nums){
                System.out.println("参数输入有误");
                return;
            }
            //创建辅助指针helper即要出圈小孩的前一个节点
            Boy helper = first;
            while (true){
                if (helper.getNext()==first){
                    break;//helper已经到达first前的节点
                }
                helper=helper.getNext();
            }
            //移动到开始报数的位置
            for (int i = 0; i <startNo-1 ; i++) {
                first = first.getNext();
                helper =helper.getNext();
            }
            //开始报数
            while(true){
                if(helper == first){//只有一个节点
                    break;
                }
                for (int i = 0; i <countNo-1 ; i++) {
                    //1、寻找待出圈的小孩、循环完成时,first就是要出圈的小孩
                    first = first.getNext();
                    helper =helper.getNext();
                }
                System.out.printf("出圈小孩编号:%d
    ",first.getNo());
                helper.setNext(first.getNext());
                first=first.getNext();
            }
            System.out.printf("最后留在圈中的小孩编号:%d
    ",first.getNo());
        }
    
    
        public void addBoy(int nums){
            if (nums<1){
                System.out.println("数量有误");
                return;
            }
            Boy curBoy =null;
            for (int i = 0; i < nums; i++) {
                Boy boy =new Boy(i+1);
                //如果是第一个小孩,指向本身
                if (i==0){
                    first=boy;
                    first.setNext(first);
                    curBoy=first;
                }else{
                    curBoy.setNext(boy);
                    boy.setNext(first);
                    curBoy=boy;
                }
            }
        }
    
        public void display(){
            if (first==null){
                System.out.println("链表为空");
                return;
            }
            Boy curBoy = first;
            while(true){
                System.out.printf("小孩的编号:%d
    ",curBoy.getNo());
                if (curBoy.getNext()==first){
                    break;
                }
                curBoy=curBoy.getNext();//后移
            }
        }
    }
    
    2.测试代码与结果

    测试类

    public class Josepfu {
    
        public static void main(String[] args) {
            CircleList c = new CircleList();
            c.addBoy(5);
            c.outOfCircle(1,2,5);
        }
    
    }
    

    运行结果

  • 相关阅读:
    Brain network involved in autonomic functions 与自主功能相关的大脑网络
    Brief summary of classical components of ERP 事件相关成分(ERP)经典成分小结
    ICA & Percentage Variance Account For (PVAF)
    数据处理中白化Whitening的作用图解分析
    Loadings vs eigenvectors in PCA 主成分分析(PCA)中的负荷和特征向量
    主成分分析(PCA)和独立成分分析(ICA)相关资料
    Sketch of heart and QRS complex 心脏及QRS波群简图
    Brain Network visulation in EEG 脑电网络可视化
    Phase Locking Value (PLV) 神经信号的锁相值
    ubuntu16.04下的一些基本操作笔记
  • 原文地址:https://www.cnblogs.com/polary/p/12868584.html
Copyright © 2011-2022 走看看