|
This is acombination generation problem.void findCombination(int n, int m) { if (n>m) findCombination(m, m); int aux[n]; memset(aux, 0, n*sizeof(int)); helper(m, 0, aux);}void helper(int dest, int idx, int aux[], int n){ if (dest == 0) dump(aux, n); if (dest <= 0 || idx==n) return; helper(dest, idx+1, aux, n); aux[idx] = 1; helper(dest-idx-1, idx+1, aux, n); aux[idx] = 0;}void dump(int aux[], int n) { for (int i=0; i<n; i++) if (aux) printf(“%3d”,i+1); printf(“\n”);}PS: this is not an elegant implementation,however, it is not necessary to use gray code or other techniques for such aproblem, right? |
|
共 1 个关于本帖的回复 最后回复于 2013-8-16 13:05