约瑟夫环问题的具体描述是:设有编号为1,2,3,..,n的n
个(n>0)个人围成一个圈,从第1
个人开始报数,报到m
时停止报数,报m
的人出圈,才从他的下一个人起重新报数,报到m
时停止报数,报m
的出圈,……,如此下去,直到所有人都出列。当任意给定n
和m
后,设计算法求n
个人出圈的次序
#include<stdio.h> //4 2 1
#include<conio.h>
#include<stdlib.h>
void main()
{
int m,n,i=0,pos=1,count=0,bao=0;//i作为指针的标记,指针值表示出圈与否,count显示出圈多少人
scanf("%d%d",&n,&m);//作为1到n,然后接上n+1到2n或者/n就能返回
int * yus;
yus=(int*)malloc(n*sizeof(int));
for (int j = 0; j < n; j++)
{
yus[j]=pos;
}//得到一个圈
while (count==n)
{
i=i%n;
if (yus[i]==pos)
{
bao++;
}
if (bao==m)
{
count++;
yus[i]=0;
bao=0;
}
i++;
}
printf("%d",i-1);
getch();
}