I have been trying to write a recursive function to print the permutations of a n-digit number.
This is the logic that i am trying to use.
1.call the permutation function,
2.keep the first digit in the first place and send the remaining 2 digits back to the permutation function recursively.
3.keep one of the two digits received and send the remaining digit to the permute function.
4. keep doing this till only one digit remains. this is the smallest solution so return it.
this is more a general idea of what i am trying to do. pls help.
int fact(int num)
{
int k=1;
if(num==0)
return 1;
while(num!=0)
{
k=k*num;
num=num-1;
}
return k;
}
int permute(int orig[],const int sizeofarr,int actualsize,int permutednumber[])
{
int temp=0;
int temparr[sizeofarr]={0};
if(sizeofarr==1)
return orig[sizeofarr-1];
else
{
for(int i=actualsize-sizeofarr;i<sizeofarr;i++)
{
permutednumber[i]=orig[i];
for(int j=0;j<sizeofarr-1;j++)
temparr[j+1]=orig[j+1];
temp=permute(temparr,sizeofarr-1,actualsize,permutednumber);
}
}
for(int i=0;i<sizeofarr;i++)
cout<<permutednumber[i];
cout<<endl;
}
int main(array<System::String ^> ^args)
{
int a[]={1,2,3};
int num[3]={0};
permute(a,3,3,num);
int i=0;
while(i!=1)
{
cin>>i;
}
return 0;
}
this is what i've been trying to do.