Josephu问题实现
我的Josephu问题Java实现。
View Code
1 /* 2 * 功能:实现Josephu算法 3 * 作者:陈沛锐 4 * 时间:2013.04.20 5 * 6 * 注意:链表指针直接是链表中的某一元素。 7 * 难点: 8 * 1.在开始数字和循环数字都为1的特殊情况下的结束条件? 9 * 2.当ChildCycle中只有一个Child是的特殊情况下的处理? 10 */ 11 package lesson9_2; 12 13 import java.util.Scanner; 14 15 public class _2josephu_second { 16 17 public static void main(String[] args) { 18 Scanner scanner=new Scanner(System.in); 19 System.out.println("请输入小孩的人数:"); 20 int childNum=scanner.nextInt(); 21 ChildCycle childCycle = new ChildCycle(childNum); 22 Child head = childCycle.createCycle(); 23 System.out.println("打印输出小孩链表:"); 24 childCycle.printChildCycle(); 25 26 System.out.println("\n请输入开始游戏的小孩编号:"); 27 int startNum=scanner.nextInt(); 28 System.out.println("请输入循环的个数:"); 29 int cycleNum=scanner.nextInt(); 30 Josephu josephu = new Josephu(); 31 josephu.startJosephu(head, startNum, cycleNum); 32 33 System.out.printf("打印输出Josephu链表:\n"); 34 josephu.printJosephu(); 35 } 36 37 } 38 39 // Child class 40 class Child { 41 int id = 0; 42 //尾指针 43 Child next = null; 44 45 public Child(int id) { 46 this.id = id; 47 } 48 } 49 50 // Child cycle 51 class ChildCycle { 52 //ChildCycle 链表的头指针 53 Child head = new Child(0); 54 //ChildCycle 链表的当前指针 55 Child point = new Child(0); 56 //ChildCycle 中 Child 的个数 57 int num = 0; 58 59 public ChildCycle(int num) { 60 this.num = num; 61 } 62 63 //创建 ChildCycle 链表 64 public Child createCycle() { 65 for (int i = 0; i < num; i++) { 66 Child child = new Child(i + 1); 67 if (i != 0) { 68 point.next = child; 69 point = child; 70 } else { 71 head = child; 72 point = child; 73 } 74 } 75 point.next = head; 76 return head; 77 } 78 79 //打印输出ChildCycle的Child 80 public void printChildCycle() { 81 point = head; 82 for (int i = 0; i < num; i++) { 83 System.out.print(point.id + " "); 84 point = point.next; 85 } 86 } 87 } 88 89 // Josephu class 90 class Josephu { 91 //josephu 链表的头指针 92 Child jhead = null; 93 //josephu 链表的当前指针 94 Child jpoint = null; 95 96 //开始形成 josephu 链表 97 public void startJosephu(Child head, int startNum, int cycleNum) { 98 Child point = head; 99 Child prevPoint = new Child(0); 100 prevPoint.next = head; 101 //当只有一个 Child 时的处理方案--------注意点 102 if(point==point.next){ 103 point.next=null; 104 jhead=point; 105 return; 106 } 107 // 移动到开始位置 108 for (int i = 0; i < (startNum - 1); i++) { 109 point = point.next; 110 prevPoint = prevPoint.next; 111 } 112 while (point != prevPoint) { 113 //移动到要出列的Child 114 for (int i = 0; i < (cycleNum - 1); i++) { 115 point = point.next; 116 prevPoint = prevPoint.next; 117 } 118 //对josephu链表的处理 119 if (jhead != null) { 120 jpoint.next = point; 121 jpoint = point; 122 } else { 123 jhead = point; 124 jpoint = point; 125 } 126 //对head所在链表的处理 127 prevPoint.next = point.next; 128 point = point.next; 129 // 在开始数字和循环数字都为1的特殊情况下的结束条件--------重难点 130 if (startNum == 1 && cycleNum == 1 && head == point.next) { 131 break; 132 } 133 } 134 //将最后出列的Child的.next置空,并链接到 josephu 链表中 135 point.next = null; 136 jpoint.next = point; 137 } 138 139 //打印输出 josephu 链表 140 public void printJosephu() { 141 jpoint = jhead; 142 while (jpoint != null) { 143 System.out.print(jpoint.id + " "); 144 jpoint = jpoint.next; 145 } 146 } 147 }
欢迎转载,欢迎批评指正!
转载请注明:
转自博客园,转载地址:http://www.cnblogs.com/igeneral/