约瑟夫问题(n个人围圈报数,报m出列,最后剩下的是几号?)

//n个人围圈报数,报m出列,最后剩下的是几号?
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int data;
    struct node *next;
}node;

node *create(int n)
{
    node *p = NULL, *head;
    head = (node*)malloc(sizeof (node ));
    p = head;
    node *s;
    int i = 1;

    if( 0 != n )
    {
        while( i <= n )
        {
            s = (node *)malloc(sizeof (node));
            s->data = i++;    // 为循环链表初始化,第一个结点为1,第二个结点为2。
            p->next = s;
            p = s;
        }
        s->next = head->next;
    }

    free(head);

    return s->next ;
}

int main()
{
    int n = 41;
    int m = 3;
    int i;
    node *p = create(n);
    node *temp;

    m %= n;   // m在这里是等于3

    while (p != p->next )
    {
        for (i = 1; i < m-1; i++)
        {
            p = p->next ;
        }

        printf("%d->", p->next->data );

        temp = p->next ;                //删除第m个节点
        p->next = temp->next ;
        free(temp);

        p = p->next ;
    }

    printf("%d\n", p->data );

    return 0;
}

增加难度:

/*编号为1~N的N个人按顺时针方向围坐一圈,每人持有一个密码(正整数,可以自由输入), 
开始人选一个正整数作为报数上限值M,从第一个人按顺时针方向自1开始顺序报数,报道M时停止报数。 
报M的人出列,将他的密码作为新的M值,从他顺时针方向上的下一个人开始从1报数,如此下去,直至所有人全部出列为止。*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_NODE_NUM 100
#define TRUE 1U
#define FALSE 0U

typedef struct NodeType
{
    int id;
    int cipher;
    struct NodeType *next;
} NodeType;

/* 创建单向循环链表 */
static void CreaList(NodeType **, const int);
/* 运行"约瑟夫环"问题 */
static void StatGame(NodeType **, int);
/* 打印循环链表 */
static void PrntList(const NodeType *);
/* 得到一个结点 */
static NodeType *GetNode(const int, const int);
/* 测试链表是否为空, 空为TRUE,非空为FALSE */
static unsigned EmptyList(const NodeType *);

int main(void)
{
    int n, m;
    NodeType *pHead = NULL;
    while (1)
    {
        printf("请输入人数n(最多%d个): ", MAX_NODE_NUM);
        scanf("%d", &n);
        printf("和初始密码m: ");
        scanf("%d", &m);
        if (n > MAX_NODE_NUM)
        {
            printf("人数太多,请重新输入!\n");
            continue;
        }
        else
            break;
    }
    CreaList(&pHead, n);
    printf("\n------------ 循环链表原始打印 -------------\n");
    PrntList(pHead);
    printf("\n-------------删除出队情况打印 -------------\n");
    StatGame(&pHead, m);
}

static void CreaList(NodeType **ppHead, const int n)
{
    int i, iCipher;
    NodeType *pNew, *pCur;
    for (i = 1; i <= n; i++)
    {
        printf("输入第%d个人的密码: ", i);
        scanf("%d", &iCipher);
        pNew = GetNode(i, iCipher);
        if (*ppHead == NULL)
        {
            *ppHead = pCur = pNew;
            pCur->next = *ppHead;
        }
        else
        {
            pNew->next = pCur->next;
            pCur->next = pNew;
            pCur = pNew;
        }
    }
    printf("完成单向循环链表的创建!\n");
}

static void StatGame(NodeType **ppHead, int iCipher)
{
    int iCounter, iFlag = 1;
    NodeType *pPrv, *pCur, *pDel;
    pPrv = pCur = *ppHead;
    /* 将pPrv初始为指向尾结点,为删除作好准备 */
    while (pPrv->next != *ppHead)
        pPrv = pPrv->next;
    while (iFlag)
    {
        for (iCounter = 1; iCounter < iCipher; iCounter++)
        {
            pPrv = pCur;
            pCur = pCur->next;
        }
        if (pPrv == pCur)
            iFlag = 0;
        pDel = pCur; /* 删除pCur指向的结点,即有人出列 */
        pPrv->next = pCur->next;
        pCur = pCur->next;
        iCipher = pDel->cipher;
        printf("第%d个人出列, 密码: %d\n", pDel->id, pDel->cipher);
        free(pDel);
    }
    *ppHead = NULL;
    getchar();
}

static void PrntList(const NodeType *pHead)
{
    const NodeType *pCur = pHead;
    if (EmptyList(pHead))
        return;
    do
    {
        printf("第%d个人, 密码: %d\n", pCur->id, pCur->cipher);
        pCur = pCur->next;
    }
    while (pCur != pHead);
    getchar();
}

static NodeType *GetNode(const int iId, const int iCipher)
{
    NodeType *pNew;
    pNew = (NodeType *)malloc(sizeof(NodeType));
    if(!pNew)
    {
        printf("Error, the memory is not enough!\n");
        exit(-1);
    }
    pNew->id = iId;
    pNew->cipher = iCipher;
    pNew->next = NULL;
    return pNew;
}

static unsigned EmptyList(const NodeType *pHead)
{
    if(!pHead)
    {
        printf("The list is empty!\n");
        return TRUE;
    }
    return FALSE;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 为间隔,最后一个序号后面没有空格。注意输出的序号应该从1开始。 【样例输入】 6 5 【样例输出】 5 4 6 2 3 1 【样例说明】 6个人成一圈,从1开始报数,每报数到5的人出列最后剩下1个人。出列的顺序依次为5,4,6,2,3,1。 ### 回答2: 问题是一个经典的数学问题。根据题意,我们需要将N个人依次编号为1到N,并且按照报数的顺序逐个淘汰,直到最后剩下一个人。 解题思路如下: 1. 首先创建一个长度为N的列表lst,用来存储参与游戏的所有人的编号。初始化列表中的元素为1到N。 2. 创建一个变量count,用来记录当前报数的人的位置。 3. 根据M的值,进行循环淘汰的过程,直到剩下最后一个人。 3.1. 当count等于M时,将lst中的元素打印,并将其从列表中删除。 3.2. 如果count小于M,则将count加1。 3.3. 如果count等于M,将count重置为1,即从新的位置重新开始报数。 4. 循环结束后,剩下最后一个人的编号即为答案,将其打印输出。 下面是用Python实现的代码: ```python N, M = map(int, input().split()) lst = [i for i in range(1, N + 1)] # 创建参与游戏的人的列表 count = 1 # 当前报数的人的位置 while len(lst) > 1: if count == M: print(lst.pop(0), end=' ') count = 1 else: lst.append(lst.pop(0)) count += 1 print(lst[0]) ``` 以上代码通过创建一个长度为N的列表来模拟游戏的过程,在每次报数到M时,将对应的人从列表中删除。最后剩下的一个人即为答案。 ### 回答3: 问题是一个经典的数学问题。要解决该问题,可以使用循环链表模拟的方法。 首先建立一个有N个节点的循环链表,将每个节点的值设置为对应的序号。然后从第一个节点开始,进行报数和删除操作,直到只剩下一个节点为止。 具体的步骤如下: 1. 输入N和M,创建一个长度为N的循环链表。 2. 创建一个指针指向循环链表的头部。 3. 从第一个节点开始,进行报数操作。 4. 报数到M时,将当前节点删除,并输出其序号。 5. 将指针指向下一个节点。 6. 重复步骤3-5,直到只剩下一个节点。 7. 输出最后剩下的节点的序号。 以N=6,M=5为例,初始的循环链表如下:1->2->3->4->5->6->1 开始报数操作: 报数到5时,删除节点5,输出5;循环链表变为:1->2->3->4->6->1 继续报数报数到5时,删除节点1,输出1;循环链表变为:2->3->4->6->2 报数到5时,删除节点2,输出2;循环链表变为:3->4->6->3 报数到5时,删除节点3,输出3;循环链表变为:4->6->4 报数到5时,删除节点4,输出4;循环链表变为:6->6 报数到5时,删除节点6,输出6;循环链表变为:6 最后剩下的节点是6,输出6。 所以,按照N=6,M=5的规则,依次出列的序号是:5 1 2 3 4 6

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值