13个人围成一圈,从第1个人开始顺序报号1、2、3,凡报到3的人退出圈子。

该程序使用C语言创建了一个环形链表,模拟13人报数淘汰游戏。每个节点代表一个人,编号从1到13。当节点报数为3时,该节点退出(将编号设为0)。程序输出淘汰顺序和最后留在链表中的人的原始编号。

13个人围成一圈,从第1个人开始顺序报号1、2、3,凡报到3的人退出圈子。找出最后留在圈子里的人原来的序号。要求用结构体编程实现。

输出提示 "出圈成员及顺序:" 格式 "%3d"

输出提示 "\n最后的成员是:" 格式 "%3d"

程序的运行示例如下:

出圈成员及顺序: 3 6 (后面以此类推,抱歉这里的输出结果保密,暂不公开,注意最后一个人是不出圈的哦)

最后的成员是: (抱歉这里的输出结果保密,暂不公开)

思路:

创建一个环形链表,给链表中的每一个节点从1~13编号,然后开始淘汰过程,对于淘汰的节点,序号置为0,淘汰完成之后,找到序号不为0的即为最后留下的。

#include<stdio.h>
#define NUM 13
typedef struct people
{
    int num;
    struct people* next;
} people;
int main()
{
    people arr[NUM];
    people*head = arr;
    for (int i = 0; i < NUM; i++)
    {
        head->num = i + 1;
        head->next = &arr[i + 1];
        head = head->next;
    }
    arr[NUM - 1].next = arr;
    int count = NUM;
    int i = 1;
    head = arr;
    printf("出圈成员及顺序:");
    while (count > 1)
    {
        if (head->num == 0)
        {
            head = head->next;
            continue;
        }
        if (i == 3)
        {
            printf("%3d", 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("%3d", head->num);
            break;
        }
    }
    return 0;
}
以下是几种使用C语言通过链表实现解决13个人围成一圈,从第1个人开始顺序报号1,2,3,凡报到3退出圈子找出最后留在圈子中的原来序号的代码示例: #### 示例一 ```c #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, res; for (arr* temp; p->next != p; p = p->next) { printf("%d\t", p->num); if (k == 2) { temp = p->next; p->next = temp->next; free(temp); k = 1; continue; } k += 1; } res = p->num; free(p); return res; } int main() { arr* p; printf("初始号数为\n"); p = creat(); printf("\n边报数边退出:\n"); printf("\n最后剩下的为最初的%d号\n", fun(p)); system("pause"); return 0; } ``` 此代码先创建一个包含13个节点的环形链表,每个节点代表一个人并编号。然后开始报数,报到3的节点被移除,直到链表中只剩一个节点,该节点的编号即为最后留下的原序号[^1]。 #### 示例二 ```c #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; } ``` 该代码同样创建环形链表,给每个节点编号。在报数过程中,将报到3的节点编号置为0表示淘汰,最后找出编号不为0的节点,其编号即为所求[^2]。 #### 示例三 ```c #include <stdio.h> #define N 13 struct person { int number; int nextp; } link[N + 1]; int main() { int i, count, h; for (i = 1; i <= N; i++) { if (i == N) link[i].nextp = 1; else link[i].nextp = i + 1; link[i].number = i; } printf("\n"); count = 0; h = N; printf("sequence that persons leave the circle:\n"); while (count < N - 1) { i = 0; while (i != 3) { h = link[h].nextp; if (link[h].number) i++; } printf("%4d", link[h].number); link[h].number = 0; count++; } printf("\nThe last one is"); for (i = 1; i <= N; i++) if (link[i].number) printf("%3d", link[i].number); printf("\n"); return 0; } ``` 此代码使用结构体数组模拟链表,每个元素包含编号和指向下一个元素的索引。通过循环报数,将报到3的元素编号置为0,最后找出编号不为0的元素,其编号就是最后留下的原序号[^3]。 #### 示例四 ```c #include <stdio.h> #include <malloc.h> typedef struct z { int data; struct z *next; } zz; int remain(int n); int main(void) { int num; do { printf("Please input n:"); scanf("%d", &num); } while (num < 3 || num > 200); printf("\nResult is:\n"); printf("The remained student is %d.\n", remain(num)); return 0; } int remain(int num) { int i, j; zz *head, *tail; head = tail = (zz *)malloc(sizeof(zz)); for (i = 1; i < num; i++) { tail->data = i; tail->next = (zz *)malloc(sizeof(zz)); tail = tail->next; } tail->data = i; tail->next = head; for (i = 1; tail != head; i++) { for (j = 1; j < 3; j++) { tail = head; head = head->next; } tail->next = head->next; printf("delete %d student %d.\n", i, head->data); free(head); head = tail->next; } i = head->data; free(head); return i; } ``` 该代码创建环形链表,通过循环报数移除报到3的节点,直到链表中只剩一个节点,返回该节点的数据作为结果[^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值