求用C语言编写的约瑟夫环问题

最好有注释
谢谢

第1个回答  2006-10-25
2.有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

#include<stdio.h>
#include<malloc.h>
#define Namelength 10
typedef struct CLNode{
char*name;
int ID;
int pastword;
struct CLNode *next;
}child,*ptrchild;
ptrchild CreateCList(int n)
{int i;
ptrchild p,head,rear;
printf("please input the children information:");
rear=head=(ptrchild)malloc(sizeof(child));
rear->next=head;

for(i=0;i<n;i++)
{getchar();
p=(ptrchild)malloc(sizeof(child));
p->ID=i+1;
p->name=(char*)malloc(Namelength*sizeof(char));
gets(p->name);
scanf("%d",&(p->pastword));
rear->next=p;
rear=p;
}
rear->next=head;
return head;
}

Joshpus(ptrchild head,int m)
{ptrchild p,q;int i=0;
p=head;
while(head->next!=head)
{while(i<m)
{q=p;
p=p->next;
if(p==head){q=head;p=p->next;}
i++;
}
printf("\nID:%d Name:%s",p->ID,p->name);
q->next=p->next;
m=p->pastword;
i=0;
free(p);
p=q;
}
printf("\n");
}

main()
{ptrchild CL;
int n,m;
printf("please input the number of children:");
scanf("%d",&n);
CL=CreateCList(n);
printf("please input the initial value of m:");
scanf("%d",&m);
Joshpus(CL,m);
}
第2个回答  2006-10-24
呵呵 长见识了

约瑟夫环本回答被提问者采纳
第3个回答  2006-10-24
约瑟夫环?
有没有搞错?
这么专业的问题,能听说过的人都不多的
你不给分就想要代码?
我有个大学同学就是搞这方面的,他有很多关于这些的代码
(不过不是C 的,哈哈)
相似回答