# 单循环链表与约瑟夫问题模拟（java）

0 0

6 2
12 4
8 3
0 0

5
1
7

```import java.util.Scanner;

public class Main< T> {
private int size = 0;

public Main() {
current=tail;

}
if (size == 0) {
head = new Node< T>(data, null);
current=tail;

} else {
tail.next = new Node< T>(data, head);
tail = tail.next;
current=tail;
}
size++;
}

public T moveANDremove(boolean flag){
T data=current.next.getData();
if(flag) {
}else if(current.next==tail){
tail=current;
}
current.next=current.next.next;

size--;
return data;
}else{
current=current.next;
return data;
}

}

public int size(){
return size;
}

public String toString(){
if (size < 1) {
return "没有元素";
} else {
StringBuffer sb=new StringBuffer();
do{
sb.append(tmp.getData()+"  ");
tmp = tmp.next;
return sb.toString();
}
}

public static void main(String[] args) {
Scanner in=new Scanner(System.in);

while(true){
int k=1;
int n=in.nextInt();
int m=in.nextInt();
if(n==0&m==0) break;
Main< Integer> l = new Main< Integer>();
for(int i=1;i<=n;i++)

while(true){

if(k%m!=0) {
l.moveANDremove(false);
k++;
}
else{
l.moveANDremove(true);
k=1;
}

if(l.size()==1) {
System.out.println(l.current.getData());
// System.out.println(l);
break;
}
}
}
}

}

class Node< T> {
T data;
Node< T> next;
public Node(T data, Node< T> next) {
this.data = data;
this.next = next;
}
public T getData(){
return data;
}
public void setData(T data){
this.data=data;
}
}
```

• 0

开心

• 0

板砖

• 0

感动

• 0

有用

• 0

疑问

• 0

难过

• 0

无聊

• 0

震惊