题目
#include<bits/stdc++.h>
using namespace std;
typedef struct DNode{
int data;
struct DNode *pre,*next;
}DNode;
DNode *phead;
void headinsert(int x){
DNode *pTemp=(DNode *)malloc(sizeof(DNode));
pTemp->data=x;
pTemp->next=phead->next;
pTemp->pre=phead;
phead->next->pre=pTemp;
phead->next=pTemp;
return;
}
void printlist(){
DNode *p=phead->next;
while(p!=phead){
cout << p->data << " ";
p=p->next;
}
cout << endl;
return;
}
void detele(){
DNode *q=phead->next;
phead->next=q->next;
q->next->pre=phead;
free(q);
return;
}
void tailinsert(int x){
DNode *p=phead->next;
while(p->next!=phead) p=p->next;
DNode *pTemp=(DNode *)malloc(sizeof(DNode));
pTemp->data=x;
pTemp->next=p->next;
p->next=pTemp;
return;
}
int main(){
phead=(DNode *)malloc(sizeof(DNode));
phead->next=phead;
phead->pre=phead;
int n,k;
cin >> n >> k;
for(int i=n;i>=1;i--) headinsert(i);
int dir=1,cnt=0;
DNode *p=phead->next;
while(phead->next->next!=phead){
cnt++;
if(cnt==k){
detele();
cnt=0;
}else{
if(dir==1) tailinsert(p->data);
else headinsert(p->data);
detele();
}
if((p->pre==phead&&dir==-1)||(p->next==phead&&dir==1)){
dir=-dir;
}else{
if(dir==1) p=p->next;
else p=p->pre;
}
}
cout << phead->next->data;
return 0;
}