void JosePhus(int n,int m,int k){
Node *head = new Node;
head->data = 0;
head->next = head;
Node *cur = head;
for(int i = 0 ; i < n ; ++i){
Node *temp = new Node;
temp->data = i;
temp->next = head;
cur->next = temp;
cur = temp;
}
for(Node *p = head->next ; p!=head; p = p->next)
cout<<p->data<<endl;
Node *p = head->next;
Node *r;
while(k--){
r = p;
p = p->next;
}
while(n--){
for(int s = m-1;s--;r=p,p=p->next);
r->next = p->next;
cout<<p->data<<endl;
free(p);
p = r->next;
}
}
|