A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

今天看了一道题,差点没吐了,总算明白了,跟大家分享下
由n个小孩围成一个首尾相连的圈报数。
*从第k个人开始,从1开始报数,报到m的人出圈,
*剩下的人继续从1开始报数,直到所有的人都出圈为止。
*对于给定的k,m和n,求出最后出圈的小孩.
  1. package com.itcast.cyclink;

  2. public class Test1 {
  3.         public static void main(String[] args) {
  4.                 CycLink cycLink = new CycLink();
  5.                 cycLink.setLen(100);
  6.                 cycLink.createLink();
  7.                 cycLink.setK(1);
  8.                 cycLink.setM(14);
  9.                 //cycLink.show();
  10.                 cycLink.play();
  11.                
  12.         }

  13. }

  14. class Child{
  15.         int no;
  16.         Child nextChild = null;
  17.         public Child(int no){
  18. //                给一个编号
  19.                 this.no = no;
  20.         }
  21.        
  22. }
  23. //环形链表
  24. class CycLink{
  25.        
  26. //        先定义一个指向链表第一个小孩的引用
  27. //        第一个小孩是不能动的
  28.         Child firstchild = null;
  29.         Child temp = null;
  30.         int len = 0;//表示共有几个小孩
  31.         int k = 0;
  32.         int m = 0;
  33.        
  34.        
  35. //        设置链表大小
  36.         public void setLen(int len){
  37.                 this.len= len;
  38.         }
  39. //        设置从第几个人开始数数
  40.         public void setK(int k){
  41.                 this.k = k;
  42.         }
  43.         //        设置数m下
  44.         public void setM(int m){
  45.                 this.m = m;
  46.         }
  47.         //开始play
  48.         public void play(){
  49. //                首先找到跑龙套的人
  50.                 Child temp = this.firstchild;               
  51. //                1.先找到开始数数的人
  52.                 for (int i = 1; i < k; i++) {
  53.                         temp = temp.nextChild;
  54.                 }
  55.                 while(this.len!=1){
  56.                        
  57. //                2.数m下
  58.                         for (int j = 1; j < m; j++) {
  59.                                
  60.                                 temp = temp.nextChild;
  61.                         }
  62. //                找到要出圈的前一个人
  63.                         Child temp2 = temp;
  64.                         while (temp2.nextChild!=temp) {
  65.                                 temp2 = temp2.nextChild;
  66.                                
  67.                         }
  68. //                3.将数到m的小孩退出圈
  69.                         temp2.nextChild=temp.nextChild;
  70. //                让temp指向下一个数数小孩
  71.                         temp = temp.nextChild;
  72.                         this.len--;
  73.                        
  74.                 }
  75. //                最后一个小孩
  76.                 System.out.println("最后出圈的小孩是:"+temp.no);
  77.                
  78.         }
  79. //        初始化环形链表
  80.         public void createLink(){
  81.                 for (int i = 1; i <= len; i++) {
  82.                         if (i==1) {
  83. //                                创建第一个小孩
  84.                                 Child ch = new Child(i);
  85.                                 this.firstchild = ch;
  86.                                 this.temp = ch;
  87.                        
  88.                         }else {
  89. //                                创建最后一个小孩
  90.                                 if (i==len) {
  91. //                                        继续创建小孩
  92.                                         Child ch = new Child(i);
  93.                                         temp.nextChild = ch;
  94.                                         temp=ch;
  95.                                         temp.nextChild = this.firstchild;
  96.                                 }else {
  97. //                                        继续创建小孩
  98.                                         Child ch = new Child(i);
  99.                                         temp.nextChild=ch;
  100.                                         temp = ch;
  101.                                 }
  102.                         }
  103.                        
  104.                 }
  105.         }
  106. //        打印环形链表
  107.         public void show(){
  108. //                定义一个跑龙套
  109.                 Child temp = this.firstchild;
  110.                 do {
  111.                         System.out.print(temp.no);
  112.                         temp=temp.nextChild;
  113.                 } while (temp != this.firstchild );
  114.         }
  115. }
复制代码

1 个回复

倒序浏览
学习学习!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马