⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 123.c

📁 约瑟夫环。。。对网上的一个程序的优化与修改。。实现的是循环链表下。。每个人不同的密码出列的问题
💻 C
字号:
#include <stdio.h>      
#include "stdlib.h"   
      
struct Node   
{      
    int num;
    int data;
    struct Node *link;
};
      
void main()      
{       
    struct Node *h, *u, *p,*o;
    int n, m, i,r=0;
    char q;


    printf("Please input how many people join this game:\n");
    scanf("%d",&n);
    getchar();
    printf("Now,which one you want to be the first man to leave:\n");
    scanf("%d",&m);
    h = u = (struct Node*)malloc(sizeof(struct Node));
    h->num = 1;       /*标记头结点*/
    for(i = 2; i <= n; i++)     /*创建连表*/
    {      
        u->link = (struct Node*)malloc(sizeof(struct Node));      
        u = u->link;      
        u->num = i;    
    }      
    u->link = h; /*构成循环*/



    printf("Ok,let's give everyone a key:\n");
    o=h;
    for(i=1;i<=n;i++)
        {
           printf("The key of the people%d:",i);/*向每一个人赋密码值*/
                      scanf("%d",&o->data);
                      o=o->link;
        }



    system("clr");
    do{
    printf("\nOK,do you want to know the sequence of the leave people?('Y'\'N'):");
    scanf("%c",&q);
    if(q=='Y'||q=='y')        r=1;else r=0;
       }
    while(!r);




    while (n)      
    {      
        for(i = 1; i < m; i++)    
            u = u->link;    /*找到出对的人的前一个*/
            p = u->link;    /*p指向要出对的人*/
            u->link = p->link;  /*u指向出队的下一个*/
            printf("%4d", p->num);
            m=p->data;
            free(p);
        n--;      
    }   
    printf("\n\n");
    getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -