13个人围成环,123报数 报到3的人退出,最后剩一人结束。用链表实现

#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; //head 指向p[0]
//1~13编号
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 = 0;
head = p;
while (count > 1)
{
    //跳过已经被淘汰的节点
	if (head->num == 0)
	{
		head = head->next;
		continue;
	}

else
{
i++;. //报数
if (i == 3)
{
//被淘汰的节点,num置为0
printf(“第 %d 位置被淘汰\n”, head->num);
head->num = 0;
count–;
i=0; //计数器归0
}
head = head->next;
}
}
printf("--------------\n");
while (head->num == 0)
{
//非0节点即为最后留下的
head = head->next;
if (head->num != 0)
{
printf(“留到最后的是 %d \n”, head->num);
}
}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值