鱼C论坛

 找回密码
 立即注册
查看: 3952|回复: 2

[学习笔记] 用Java实现约瑟夫环的。(重新发一遍诶。。。)

[复制链接]
发表于 2020-6-10 21:57:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. //作者:铁头娃鸭
  2. //日期:2020.6.9
  3. //作用:实现约瑟夫环
  4. public class work6{
  5. public static void main(String args[]){
  6.    CycLink cyclink=new CycLink();
  7.    cyclink.setlen(8);//长度
  8.    cyclink.setk(4);//从第几个小孩开始数
  9.    cyclink.setm(4);//数多少下
  10.    cyclink.create_link();
  11.    cyclink.show();
  12.    cyclink.play();
  13.    
  14. }
  15. }//小孩的类
  16. class Child {
  17.   //编号
  18.   int no;
  19.   //暂时不知道指向那个孩子所以置空,
  20.   Child nextchild=null;
  21.   public Child (int no){
  22.    this.no=no;
  23.   }
  24. }
  25. //环形链表
  26. class CycLink{
  27. // 先定义一个指向链表第一个小孩的的引用(头结点)
  28. Child first_child=null;
  29. Child temp=null;//跑龙套的temp
  30. Child  tem=null;//跟着跑龙套的后面的
  31. int len=0;//表示共有几个小孩
  32. int k=0;//从第几个小孩开始数
  33. int m;//数多少下
  34.     //开始创建小孩
  35.   public void create_link(){
  36.       for(int i=1; i<=len;i++){
  37.       if(i==1){ //创建第一个小孩
  38.       Child ch=new Child(i);  
  39.        this.first_child=ch;
  40.        this.temp=ch;
  41.            }  
  42.         else {
  43.             if (i==len){//最后一个小孩
  44.              Child ch=new Child(i);
  45.               temp.nextchild=ch;
  46.                   temp=ch;
  47.                temp.nextchild=this.first_child;
  48.                  }
  49.              else {//其余小孩
  50.             Child ch=new Child(i);
  51.             temp.nextchild=ch;
  52.              temp=ch;
  53.                     }
  54.              }
  55.         }
  56.       }
  57.    //开始游戏
  58. public void play(){
  59.           //让tem指向第一个小孩的后面,也就是最后一个小孩(本意就是让tem一直跟在temp后面,这样方便施行让小孩退圈的操作)
  60.             this.tem=this.first_child;
  61.         for(int j=1; j<len;j++){
  62.                 tem=tem.nextchild;
  63.                             }
  64.              Child temp=this.first_child;
  65.      //1找到数数的人从第k个小孩开始数
  66.      for (int i=1;i<k;i++){
  67.         
  68.         tem=tem.nextchild;
  69.         temp=temp.nextchild;
  70.      }
  71.      //数m下;
  72.        while(len!=1){
  73.        for (int i=1;i<=m;i++){
  74.           tem=tem.nextchild;
  75.           temp=temp.nextchild;
  76.          }
  77.     //将数到m下的小孩退出圈   
  78.         System.out.print("推出圈的小孩:"+temp.no+" ") ;
  79.         tem.nextchild=temp.nextchild;
  80.         temp=temp.nextchild;
  81.           len--;
  82.             }  
  83.           System.out.println("   "+temp.no);     
  84.    }
  85. //初始化循环链表
  86.    public void setk(int k){
  87.     this.k=k;
  88. }
  89. public void setm(int m){
  90.     this.m=m;
  91. }
  92. public void setlen (int len){
  93.    this.len=len;
  94.   }

  95.         public void show(){
  96.               Child temp1=this.first_child;
  97.                do{
  98.                   System.out.print(temp1.no);
  99.                        temp1=temp1.nextchild;
  100.                       }while (temp1!=first_child);
  101.             }
  102. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-10 22:11:32 | 显示全部楼层
还不错哦,继续加油
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-10 23:29:26 | 显示全部楼层
加油
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-16 01:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表