• java===java习题---Josephu问题


    package testbotoo;
    /**
     * 
     * @author 
     */
    
    public class Demo4 {
        public static void main(String[] args)
        {
            CycLink cyclink = new CycLink();
            cyclink.setLen(10);
            cyclink.createLink();
            cyclink.setK(2);
            cyclink.setM(2);
            cyclink.show();
            cyclink.play();
        }
    
    }
    
    
    class Child
    {
        int num;
        Child nextChild = null;
        
        public Child(int num){
            this.num = num;
        }
    }
    
    
    class CycLink
    {
        //先定义一个指向链表第一个小孩的应用
        Child firstChild = null;
        
        Child temp = null;
        int len = 0; //表示共有几个小孩
        int k = 0;
        int m = 0;
        
        //设置从第几个人开始数数
        public void setK(int k)
        {
            this.k = k;
        }
        
        public void setM(int m)
        {
            this.m = m;
        }
        
        public void setLen(int len)
        {
            this.len = len;
        }
    
        //开始play
        public void play()
        {
            Child temp = this.firstChild;
            //1.先找到开始数数的人
            for(int i = 1; i<k;i++)
            {
                temp = temp.nextChild;
            }
            while(this.len!=1)
            {
                //2.数m下
                for(int j=1;j<m;j++ )
                {
                    if(j<m-1)
                    {
                        temp = temp.nextChild;
                    }else
                    {    //数到最后一下,打印要出圈的孩子的num
                        temp = temp.nextChild;
                        System.out.println("要出圈的小孩是"+temp.num);
                    }
                }
                //找到要出圈的前一个小孩
                Child temp2 = temp;
                while(temp2.nextChild!=temp)
                {
                    temp2 = temp2.nextChild;
                    
                }
                
                //3.将数到m的小孩推出圈
                temp2.nextChild = temp.nextChild;
                temp=temp.nextChild;
                this.len--;
            }
            //最后一个小孩
            System.out.println("最后剩下的小孩是:"+temp.num);
        }
        
        //初始化环形链表
        public void createLink()
        {
            for(int i = 1; i <= len; i++)
            {
                if (i==1)
                {
                    //创建第一个小孩
                    Child ch = new Child(i);
                    this.firstChild = ch;
                    this.temp =ch;
                }else  if(i == len)
                {
                    //创建最后一个小孩
                    Child ch = new Child(i);
                    temp.nextChild = ch;
                    temp =ch;
                    temp.nextChild = this.firstChild;
                }else
                {
                    Child ch = new Child(i);
                    temp.nextChild = ch;
                    temp = ch;
                    
                }
            }
        }
        
        public void show()
        {
            //定义一个跑龙套的
            Child temp = this.firstChild;
            do{
                System.out.println(temp.num);
                temp = temp.nextChild;
            }while(temp!=this.firstChild);
        }
    }
  • 相关阅读:
    Crontab '2>&1 &' 含义
    form提交方式Methor
    oracle基本术语
    在工作中常用的sql语句
    常用的删除大数据方法(游标+分段)
    oracle9i、10g、11g区别
    SSH面试总结(Hibernage面试)
    实习生招聘笔试
    TopCoder上一道600分滴题
    Oracle数据库面试题汇总
  • 原文地址:https://www.cnblogs.com/botoo/p/8796529.html
Copyright © 2020-2023  润新知