韩顺平老师学java——Java实现丢手帕问题约瑟夫

本文介绍了一种基于双向队列的逻辑优化方案,通过改进算法将处理9万个元素的时间从15秒缩短到了2秒左右,显著提升了执行效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在韩老师的基础上添加了双向队列逻辑标红部分,提高了执行效率,9W个处理时间由之前的15S左右提高到2S左右,提升还是很明显的

 

package com.kewin;

public class Demo4 {
    
    public static void main(String[] args)
    {
        long starttime = System.currentTimeMillis();
        Cyslink cyc= new Cyslink();
        cyc.SetChild(90000);
        cyc.createlink();
        cyc.show();
        cyc.SetK(2);
        cyc.SetM(2);
        cyc.play();
        long time2 = System.currentTimeMillis();
        System.out.println("玩游戏共用了" + (time2 - starttime)/1000.0 + "秒");
    }

}
    
    
class Child
    {
        int no;
        Child lastchilae=null;
        Child nextchilae=null;
        public Child(int a )
        {
            this.no=a;
        }
        
    }
    
     class Cyslink
    {
        Child FirstChild=null;
        Child temp =null;
        Child Lastchild=null;
        int len=0;
        int k =0;
        int m =0;
        
        public void SetChild(int len)
        {
            this.len=len;
        }
        
        public void SetK(int k)
        {
            this.k=k;
        }
        
        
        public void SetM(int m)
        {
            this.m=m;
        }
        
        
         public void createlink()
        {
            for (int i=1;i<=len;i++)
            {
                
                if (i==1) {
                    Child ch =new Child(i);
                    this.FirstChild=ch;
                    this.temp=ch;
                    this.Lastchild=ch;
                }else 
                { 
                    if (i==len)
                {
                     Child ch =new Child(i);
                    temp.nextchilae=ch;
                    temp =ch;
                    temp.lastchilae=this.Lastchild;
                    temp.nextchilae=this.FirstChild;
                    FirstChild.lastchilae=ch;
                    
                }
                else
                     {
                    Child ch =new Child(i);
                    temp.nextchilae=ch;
                    temp =ch;
                    temp.lastchilae=this.Lastchild;
                    this.Lastchild=ch;

                    }
                }
            }
        }
        
        public void play()
        {
            Child tmpchilde=this.FirstChild;
            for (int i=1;i<this.k;i++)
            {
                tmpchilde=tmpchilde.nextchilae;
            }
            while (this.len !=1)
            {
            for (int i =1 ;i<this.m;i++)
            {
                tmpchilde=tmpchilde.nextchilae;
            }
//            Child tmp22 =tmpchilde.nextchilae;
//            while (tmp22.nextchilae != tmpchilde)
//            {
//                tmp22=tmp22.nextchilae;
//            }
//            tmp22.nextchilae=tmpchilde.nextchilae;
            
            System.out.println(tmpchilde.no+ "出局");
            tmpchilde.lastchilae.nextchilae=tmpchilde.nextchilae;

            tmpchilde.nextchilae.lastchilae=tmpchilde.lastchilae;

            tmpchilde = tmpchilde.nextchilae;
            this.len--;
            }
            System.out.println(tmpchilde.no);
        }
        
        public void show()
         {  
             
                Child temChild=this.FirstChild;  
                do {  
                    System.out.println(temChild.no+"####"+temChild.nextchilae.no+"####"+temChild.lastchilae.no);  
                    temChild=temChild.nextchilae;  
                } while (temChild!=this.FirstChild);  
            }  
        
    }

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值