How to transform the several loops code of finding combination into
recursive method?
I am trying to convert this code into recursive method
for(int i=0;i<25;++i)
for(int j=i+1;j<25;++j)
for(int k=j+1;k<25;++k)
for(int l=k+1;l<25;++l)
for(int m=l+1;m<25;++m)
{//}
the method is finding the 25c5 combinations. In recursive way I have
written this
int soln[5];
void backtrack(int c)
{
if(c<5)
{
for(int i=c;i<25;++i)
{
soln[c] = i;
backtrack(c+1);
}
}
else
{ // }
my soln is clearly wrong cause the number of recursion is 6 million but
the actual should be 50 thousand. How do I correct that?
No comments:
Post a Comment