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

📄 jocef.c

📁 约瑟夫环的实现,在编程当中时常出现的,相信大家都有所了解的.
💻 C
字号:
#include <stdlib.h>
#include <stdio.h>

typedef struct Node
{   int code;
    int num;
    struct Node *next;
}Node, *Linklist;

/*INITIAL*/
Initlist(Linklist *L)
{   *L=(Linklist)malloc(sizeof(Node));
    (*L)->next=NULL;

    return(L);
}

/*CREAT*/
CreteFromTail(Linklist  L)
{   Node *p,*r;
    int flag=1,a,b;
    r=L;
    while(flag)
    {
      if(a!=0&&b!=0)
      {
        printf("Input one of the people's number:\n");
        scanf("%d",&a);
        printf("Input his or her code:\n");
        scanf("%d",&b);
        p=(Node*)malloc(sizeof(Node));
        p->num=a;
        p->code=b;
        r->next=p;
        r=p;
      }
      else
       {
         flag=0;
         r->next=NULL;
        }

    }
    p=L;
    while(p->next->next!=NULL)
      p=p->next;
    p->next=NULL;
    r=p->next;

}

/*COUNT THE LENGTH*/
int  Lengthlist(Linklist L)
{ Node *p;
   int n=0;
    p=L->next;

    do
    { n=n+1;
      p=p->next;
    }while(p!=NULL);

  return (n);
}
/*OUTPUT*/
print(Linklist L)
{ Node *p;
  int i;
  p=L->next;
  i=1;
  while(p!=NULL)
  {
    printf("%d ",i++);
    printf("num=%d,code=%d\n  ",p->num,p->code);
    p=p->next;
  }
  getch();
}

main()
{  Node *p,*t,*q;
    int m,n;
    int i,j;
    Linklist LA;
    int a[100];

    Initlist(&LA);
    CreteFromTail(LA);
    print(LA);
    printf("The input the given code:\n");
    scanf("%d",&m);
    printf("m=%d\n",m);
    printf("The number of the people is:\n");
    n=Lengthlist(LA);
    printf("n=%d\n",n);

    q=LA->next;
    while(q->next!=NULL)
       q=q->next;
    q->next=LA;
    getch();

    p=q;
    for(i=1;i<=n;i++)
    { for(j=1;j<m;j++)
      { if(p->next!=LA)
            p=p->next;
        else
         p=p->next->next;
      }
      if(p->next==LA)
      { t=LA->next;
        a[i]=LA->next->num;
        m=LA->next->code;
        LA->next=LA->next->next;
        free(t);
      }
      else
      { t=p->next;
        a[i]=p->next->num;
        m=p->next->code;
        p->next=p->next->next;
        free(t);
      }
    }
    printf("The result is:\n");
    for(i=1;i<=n;i++)
      printf("%d ",a[i]);
     getch();
}

⌨️ 快捷键说明

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