#include <stdio.h>
#define NUM 13
typedef struct people
{
int num;
struct people *next;
} people;
int main()
{
int count = NUM;
people p[NUM];
people *head;
head = p;
for (int i = 0; i < NUM; i++)
{
head->num = i + 1;
head->next = &p[i + 1];
head = head->next;
}
p[NUM - 1].next = p;
int i = 1;
head = p;
while (count > 1)
{
if (head->num == 0)
{
head = head->next;
continue;
}
if (i == 3)
{
printf("第 %d 位置被淘汰\n", head->num);
head->num = 0;
count--;
}
head = head->next;
i++;
if (i > 3)
{
i = 1;
}
}
printf("--------------\n");
while (head->num == 0)
{
head = head->next;
if (head->num != 0)
{
printf("留到最后的是 %d \n", head->num);
}
}
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8a0a42c44f651632dc2cc8738e7e2312.png)
#include <stdio.h>
#define N 13
struct One
{
int n;
struct One *nextp;
};
int main()
{
int i,count=N;
struct One one[N];
struct One *head;
head = one;
for (i = 0; i < N; i++)
{
one[i].n = i + 1;
one[i].nextp = &one[i + 1];
}
one[N-1].nextp = &one;
i = 1;
head = one;
while (count>1)
{
if (head->n == 0)
{
head = head->nextp;
continue;
}
if (i == 3)
{
printf("out %d\n", head->n);
head->n = 0;
count--;
}
head = head->nextp;
i++;
if (i > 3)
{
i = 1;
}
}
printf("-----------\n");
while (head->n == 0)
{
head = head->nextp;
if (head->n != 0)
{
printf("the last is %d\n", (*head).n);
}
}
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5dc158eedce3ae51ca576403563a953a.png)