• 05.单向环形链表应用 -- 约瑟夫环


    
    /**
     * 单向环形链表应用 -- 约瑟夫环
     * Josephu  问题为:设编号为1,2,… n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,
     * 数到m 的那个人出列,它的下一位又从1开始报数,数到m的那个人又出列,依次类推,直到所有人出列为止,
     * 由此产生一个出队编号的序列。
     */
    public class Josepfu {
        public static void main(String[] args){
            CircleSingleLinkedList linkedList = new CircleSingleLinkedList();
            linkedList.add(5);
    //        linkedList.show();
            linkedList.countBoyNode(1,2,5);
            //从1开始~
            //2出圈
            //4出圈
            //1出圈
            //5出圈
            //圈中最后一个:3
        }
    }
    class CircleSingleLinkedList{
        private BoyNode first = null;
        //添加nums个结点
        public void add(int nums){
            if (nums<1){
                System.out.println("num");
                return;
            }
            //辅助指针
            BoyNode c = null;
            for (int i = 1; i <= nums; i++) {
                BoyNode boyNode = new BoyNode(i);
                if (i==1){
                    first = boyNode;
                    first.setNext(first);//构成环
                    c = first;//c从first开始
                }else {
                    c.setNext(boyNode);
                    boyNode.setNext(first);//环形相当于插在first前面
                    c = boyNode;
                }
            }
        }
    
        /**
         * BoyNode出圈顺序
         * @param startNo 从startNo开始数
         * @param countNum 数到countNum
         * @param nums 初始化CircleSingleLinkedList的结点数
         */
        public void countBoyNode(int startNo,int countNum,int nums){
            if (first==null||startNo<1||startNo>nums){
                System.out.println("参数有误");
                return;
            }
            BoyNode c = first;
            //找到最后一个结点
            while (true){
                if (c.getNext()==first){
                    break;
                }
                c = c.getNext();
            }
            //first移动到startNo,c为startNo的前一个
            for (int i = 1; i < startNo; i++) {
                first = first.getNext();
                c = c.getNext();
            }
            System.out.println("从"+first.getNo()+"开始~");
            while (true){
                if (c==first){//只有一个结点
                    break;
                }
                for (int i = 1; i < countNum; i++) {
                    first = first.getNext();
                    c = c.getNext();
                }
                System.out.println(first.getNo()+"出圈");
                first = first.getNext();
                c.setNext(first);
            }
            System.out.println("圈中最后一个:"+first.getNo());
        }
        public void show(){
            if (first==null){
                System.out.println("空环~");
                return;
            }
            BoyNode c = first;
            while (true){
                System.out.print(c.getNo()+"	");
                if (c.getNext()==first){
                    break;
                }
                c = c.getNext();
            }
            System.out.println();
        }
    
    }
    class BoyNode{
        private int no;
        private BoyNode next;
    
        public BoyNode(int no) {
            this.no = no;
        }
    
        public int getNo() {
            return no;
        }
    
        public void setNo(int no) {
            this.no = no;
        }
    
        public BoyNode getNext() {
            return next;
        }
    
        public void setNext(BoyNode next) {
            this.next = next;
        }
    }
    
  • 相关阅读:
    sort函数的运用
    数据库设计三大范式
    LeetCode 64. 求1+2+…+n
    LeetCode 876. 链表的中间结点
    单链表的创建和基本操作 C语言
    LeetCode 365. 水壶问题
    LeetCode 1160. 拼写单词
    LeetCode 409. 最长回文串
    LeetCode 836. 矩形重叠
    LeetCode 7. 整数反转
  • 原文地址:https://www.cnblogs.com/fly-book/p/11635550.html
Copyright © 2020-2023  润新知