import java.io.*;
class Node{
private int data[] = new int[2];
private Node link;
private static int count = -1;
public Node(int data){
this.data[0] = data;
this.data[1] = ++count;
link = null;
}
public void setData(int data){
this.data[0] = data;
}
public int getData(){
return data[0];
}
public int getCount(){
return data[1];
}
public void setLink(Node link){
this.link = link;
}
public Node getLink(){
return link;
}
}
class LinkList{
public Node head;
public Node last;
public LinkList(int data){
head = new Node(data);
head.setLink(head);
last = head;
}
public String vistAllNode(){
Node next = head;
String s = new String();
do{
s = s + next.getData() + " ";
next = next.getLink();
}while(next != head);
return s;
}
public boolean removeNode(Node node){ //去掉node的下一个结点
if(node == last){
head = head.getLink();
last.setLink(head);
}
else if(node.getLink() == last){
last = node;
last.setLink(head);
}
else{
Node tempN = node.getLink();
tempN = tempN.getLink();
node.setLink(tempN);
}
return true;
}
public boolean removeAll(){
head.setLink(head);
last = head;
head.setData(0);
return true;
}
public void append(int data){
Node temp = new Node(data);
last.setLink(temp);
last = temp;
last.setLink(head);
}
}
public class 约瑟夫环{
private static LinkList link = new LinkList(0);
private static int inCount;
private static int resultInCount;
public static void main(String []args) throws IOException{
int enter;
String inputString = new String();
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("这个程序是解决约瑟夫环的问题。/n请输入人数>>:");
inCount = Integer.parseInt(input.readLine());
System.out.print("请输入初始密码>>");
int m = Integer.parseInt(input.readLine());
System.out.println("请输入每个人的密码>>");
for(resultInCount = 0; resultInCount < inCount; resultInCount++){
inputString = input.readLine();
enter = Integer.parseInt(inputString);
link.append(enter);
}
link.removeNode(link.last);
Node p = link.head, q = link.last;
int i = 0;
int j = 0;
for(;;){
i++;
if(i == m){
i = 0;
m = p.getData();
System.out.print(p.getCount()+" ");
link.removeNode(q);
p = p.getLink();
j++;
if(j == inCount) break;
continue;
}
q = q.getLink();
p = q.getLink();
}
}
}