题目来源:码蹄集
https://matiji.net/exam/brushquestion/556/778/B3FCFEC101BD05189BB74D522E019504
参考程序:
#include <iostream>
#include <stdio.h>
#include <memory>
typedef struct NODE{
int value;
NODE *next;
} NodeList;
int main() {
int n, m;
scanf("%d %d", &n, &m);
NodeList *head, *last = NULL;
NodeList *node;
for(int i=0; i<n; ++i){
node = (NodeList*)malloc(sizeof(NodeList));
node->value = i+1;
if(i == 0){
head = node;
last = node;
}else{
last -> next = node;
last = node;
}
}
node->next = head;
last = node;
int i = 1;
while(head->next != head){
if(i == m){
NodeList *p = head;
last->next = head->next;
free(p);
i = 1;
head = last->next;
}else{
last = head;
head = head->next;
++i;
}
}
printf("%d\n", head->value);
return 0;
}