约瑟夫问题
请教:编写高效率解决约瑟夫问题的代码??
#include<iostream.h>
int Josephus(int *Child,int n,int m);
void main()
{
int *allChild,j,k,l;
cin>>j>>k;
if((allChild= new int[j])!=NULL)
{
for(l=0;l<j;l++)
{
cout<<l+1<<",";
allChild[l]=l+1;
}
cout<<endl;
cout<<Josephus(allChild,j,k);
}
}
int Josephus(int *Child,int n,int m)
{
int i=-1,j=0,k=1;
while(1)
{
for(j=0;j<m;)
{
i=(i+1)%n;
if(Child[i]!=-1)
j++;
}
if(k==n)
break;
cout<<Child[i]<<",";
Child[i]=-1;
k=k+1;
}
cout<<endl;
return(Child[i]);
}