#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct John {
int num;
struct John* next;
}John;
void Init(John* Li, int n) {//初始化
Li->num = 1;//把人编号
Li->next = Li;
int i = 2;
John* s = Li;
for (i = 2; i <= n; i++) {
s->next = (John*)malloc(sizeof(John));
s = s->next;
s->num = i;
}
s->next = Li;//构成环
}
John* Found(John* Li, int ori) {
int i = 1;
John* pj = Li;//找到起始位置
while (i < ori) {
pj = pj->next;
i++;
}
return pj;
}
John* fun(John* pj, int k) {
John* pre = NULL;
John* now = pj;
int count = 0;
while (now != now->next) {//只允许一人存活,否则循环
count++;
if (count == k) {//叫到指定数字的死亡
pre->next = now->next;//删除这个人
printf("出局者:%d号\n", now->num);
now = pre->next;//从删除的人的下一位开始报数
count = 0;
}
else
{
pre = now;//否则移动
now = now->next;
}
}
printf("\n存活者:%d号\n", now->num);
return now;
}
int main() {
John L;
John* Li = &L;
int n = 0, k = 0, ori = 0;
printf("请输入人数:\n");
scanf("%d", &n);
printf("请输入出局数:\n");
scanf("%d", &k);
printf("请输入起始点:\n");
scanf("%d", &ori);
Init(Li, n);
John* pj = Found(Li, ori);
John* ts = fun(pj, k);
free(ts);
Li = NULL;
return 0;
}