#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int num;
struct node* next;
}person;
person* initLink(int n) {
person* head = (person*)malloc(sizeof(person));
head->num = 1;
head->next = NULL;
person* cyclic = head;
for (int i = 2; i <= n; i++) {
person* body = (person*)malloc(sizeof(person));
body->num = i;
body->next = NULL;
cyclic->next = body;
cyclic = cyclic->next;
}
cyclic->next = head;
return head;
}
void findAndKillK(person* head, int k, int m) {
person* tail = head;
while (tail->next!=head)
{
tail = tail->next;
}
person* p = head;
while (p->num != k) {
tail = p;
p = p->next;
}
while (p->next!=p)
{
for (int i = 1; i < m; i++) {
tail = p;
p = p->next;
}
tail->next = p->next;
printf("出列人的编号为:%d\n", p->num);
free(p);
p = tail->next;
}
printf("出列人的编号为:%d\n", p->num);
free(p);
}
int main() {
printf("输入圆桌上的人数n:");
int n;
scanf_s("%d", &n);
person* head = initLink(n);
printf("从第K人开始报数(k>1且k<%d):", n);
int k;
scanf_s("%d", &k);
printf("数到m的人出列:");
int m;
scanf_s("%d", &m);
findAndKillK(head, k, m);
}