(C语言)13个人围成一圈,从第1个人开始顺序报号1,2,3.凡报到3者退出圈子.找出最后留在圈子中的人原来的序号,要求用链表实现
程序员文章站
2022-05-28 14:33:52
...
13个人围成一圈,从第1个人开始顺序报号1,2,3.凡报到3者退出圈子.找出最后留在圈子中的人原来的序号,要求用链表实现
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define N 13
#define M sizeof(struct arr)
struct arr {
int num;
struct arr* next;
};
typedef struct arr arr;
arr* creat() {//创建链表
arr* p = NULL;
arr* s;
arr* head = NULL;
for (int i = 0; i < N; ++i) {
s = (arr*)malloc(M);
s->num = i + 1;
printf("%d\t", s->num);
if (i == 0) {
head = s;
p = head;
}
s->next = p->next;
p->next = s;
if (i == N - 1) {
p = head;
continue;
}
p = p->next;
}
return head;
}
int fun(arr* head) {
arr* p = head;
int k = 1;
for (p = head; p->next != p; p = p->next) {
printf("%d\t", p->num);
if (k == 2) {
p->next = p->next->next;
k = 1;
continue;
}
k += 1;
}
return p->num;
}
void main() {
arr* p;
printf("初始号数为\n");
p = creat();
printf("\n边报数边退出:\n");
printf("\n最后剩下的人为最初的%d号\n", fun(p));
system("pause");
}