import com.ryan.demo11lianbiao.CircularLinkedList.Node;
public class Jospher {
public class Node{
private Node next;
private Integer value;
public Node(){}
public Node(Integer value){this.value=value;}
}
Node head = null;
public void run(int totalNum,int countNum) {
head=new Node(1);
Node temp=head;
for(int i=2;i<totalNum+1;i++) {
temp=(temp.next=new Node(i));
}
temp=temp.next=head; //重新把指针放回开头 即数值为1的位置
while(temp.next!=temp) {
for(int j=1;j<countNum-1;j++) {
temp=temp.next;
}
System.out.println(temp.next.value);
temp=temp.next=temp.next.next;
}
System.out.println(temp.value);
}
public void display(){
Node temp=head;
while(temp.next!=head){
temp=temp.next;
System.out.println(temp.value);
}
System.out.println();
}
}
public class Test {
public static void main(String[] args) {
Jospher j=new Jospher();
j.run(41,3);
//j.insertValue(1);
//j.display();
}
import java.util.LinkedList;
public class Test {
private static StringBuffer removedStr = new StringBuffer("");// 记录出列的数字
public static void main(String[] args) {
/*Jospher j=new Jospher();
j.run(41,3);*/
//j.insertValue(1);
//j.display();
long startTime = System.currentTimeMillis(); // 获取开始时间
process(41, 1, 3);
System.out.println(removedStr.substring(0, removedStr.length() - 1));
long endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间: " + (endTime - startTime) + "ms");
}
public static void cycleCal(LinkedList<Integer> list, int count, int m) {
int len = list.size();
if (len > 1) {
for (int i = 0; i < len; i++) {
if (count == m) {// 第m个时,remove
removedStr.append(list.get(i)).append(",");
list.remove(i);
len = list.size();
i--;
count = 0;
}
count++;
}
cycleCal(list, count, m);
} else {
if (len != 0) {
removedStr.append(list.get(0)).append(",");
}
}
}
public static void process(int n, int k, int m) {
// 构建一个list,存放人数
LinkedList<Integer> list = new LinkedList<Integer>();
for (int i = 0; i < n; i++) {
if (i + k > n) {
list.add(i + k - n);
} else {
list.add(i + k);
}
}
int count = 1;// 记录数的人数
cycleCal(list, count, m);
}