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

📄 yuesefu.txt

📁 在网上找的约瑟夫环大家一起参考
💻 TXT
字号:
#include<stdio.h>   

#include<stdlib.h>  

typedef   struct   Node   

{   

        int                     num;   

        int                     psw;   

        struct                  Node   *next;   

}*Joseph;   

   

  int    n;     

  int    m;      

  void   Create(Joseph   *h,int n);   

  void   Delete(Joseph   *h,int m); 

  

  void   main()   

  {   

      Joseph   *head;   

      printf("请输入人数和上限值:");   

      scanf("%d%d",&n,&m);   

      head=(Joseph*)malloc(sizeof(struct Node));   

      Create(head,n);   

      Delete(head,m);   

      free(head);   

  }   

    

  void   Create(Joseph   *h,   int  n)   

  {       

          Joseph   rear,p;   

          int   i;   

          (*h)=NULL;   

          if(n>0)   

          {   

              for(i=1;i<=n;i++)   

             {   

                   p=(Joseph)malloc(sizeof(struct  Node));   

                   p->num=i;

                   printf("请输入第%d个密码:",p->num);

                   scanf("%d",&(p->psw));

                   if(i==1)   

                   {   

                       (*h)=p;   

                       rear=p;   

                   }   

                   else   

                   {   

                       rear->next=p;   

                       rear=p;   

                   }   

              }   

              rear->next=(*h);   

         }   

  }   

    

  void   Delete(Joseph *h,int m)   

  {      

        Joseph   p,q;   

        int   i;   

        p=(*h);   

        q=(*h);   

        while(p->next!=p)   

        {      

               for(i=1;i<=m-1;i++)   

               {   

                    q=p;   

                    p=p->next;   

               }      

               printf("%d  ",p->num);   

               m=p->psw;  

               q->next=p->next;  

               free(p);   

               p=q->next;   

         }   

          printf("%d  ",p->num);   

}

⌨️ 快捷键说明

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