约瑟夫环问题,帮忙了。

来源:百度知道 编辑:UC知道 时间:2024/09/28 09:36:21
1. Josephus问题,n个人围坐成一圈,按顺序编号为1-n,确定一个整数m,从1号开始数数,每数到第m个人出列,剩下的人从下一个人重新开始数,直至只剩下一个人为止。对n=8,m=5,过程和结果如下图所示,黑色数字为编号,红色数字为出列顺序,最后剩下的是3号
这是第一部分,第0二部分请搜索“数据高手来啊” 求哥哥姐姐了。。。。

#include "alloc.h"
void main(void)
{
int Cont2,Ele,Number,Start,*Person,Spaker,Last,Cont;
do
{
printf("\n\nHow many element do you want: "); /*一共多少人*/
scanf("%d",&Ele);
printf("Which number cause Out:"); /*数到几则退出圆桌*/
scanf("%d",&Number);
printf("Which person to start:"); /*从第几个人开始数*/
scanf("%d",&Start);
}
/*当总数小于1,或者钥匙数小于1,或者开始数数的人编号比最大人数还大说明输入有误,需要重新输入*/
while(Ele<1||Number<1||Start<1||Start>Ele);

/*分配一段空间用于存储这些数如果申请空间失败则退出*/
if((Person =(int *)calloc((Ele+1),sizeof(int)))==NULL)
{
printf("\n\nOut of Memory!!!");
exit(0);
}

printf("The order is:" );
/* Set Person */
for(Cont = 1;Cont<=Ele;Cont++)
Person[Cont]=Cont+1;
Person[Ele]=1;
Spaker = Start;
/* Start say numbers */
for(Cont = 0;Co