-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJosephu.java
More file actions
51 lines (45 loc) · 1019 Bytes
/
Josephu.java
File metadata and controls
51 lines (45 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.lzz.linkedlist;
import com.lzz.vo.Node1;
public class Josephu {
CircularLinkedList cir = new CircularLinkedList();
public void addJosephu(int a) {
cir.addNode(a);
}
public void delJosephu(int a){
cir.delNode(a);
}
public void showJosephu() {
System.out.println("参与者队列:");
cir.Display();
}
public void Start(int k, int m) {
/*
* 开始求解约瑟夫问题
*/
int n = k - 1;
Node1 node = cir.findByIndex(n);
int i = 1;
while (i <= m) {
if (i == m) {
Node1 temp = node.next;
System.out.println("“" + node.getData() + "”" + "出列");
cir.delNode(node.getData());
System.out.println("队列:");
cir.Display();
node = temp;
if (cir.size == 1) {
System.out.println("“" + cir.head.getData() + "”" + "出列");
cir.delNode(cir.head.getData());
System.out.println("队列:");
cir.Display();
break;
}
i = 1;
}
else{
i++;
node = node.next;
}
}
}
}