c++ - How to transform the several loops code of finding combination into recursive method? -
i trying convert code 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 finding 25c5 combinations. in recursive way 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 wrong cause number of recursion 6 million actual should 50 thousand. how correct that?
void backtrack(int start, int depth) { if (depth < 5) { (int = start; < 25; ++i) { backtrack(i + 1, depth + 1); } } }
seems same nested loops.
Comments
Post a Comment