7.3.1 Incremental Construction method
Idea: Select one element at a time to put it in the collection. My own understanding of recursion is not enough, although there is no explicit recursive stop condition, but if you can not continue to add elements, it will not continue to recursion, and then my headache is backtracking.
#include <stdio.h>intnum[4],n;voidAintNint*a,intans) { for(inti =0; i < ans; i + +)//Print Current elementprintf"%d", A[i]); printf ("\ n"); ints = ans?a[ans-1]+1:0;//determines the minimum possible value of the current element for(inti = s; I < n; i + +) {A[ans]=i; A (N,a,ans+1);//Recursive constructed subset } return;}intMain () {n=3; A (N,num,0); return 0;}
7.3.2-bit vector method
Idea: Constructs a bit vector a[i], if a[i]=1, if and only if I is in subset A of the set.
#include <stdio.h>intnum[4],n;voidPrint_subset (intNint*a,intans) { if(ans = =N) { for(inti =0; i < ans; i + +)//Print the current collection if(A[i]) printf ("%d", i); printf ("\ n"); return; } A[ans]=1;//Select cur elementPrint_subset (n,a,ans+1); A[ans]=0;//do not select the first CUR elementPrint_subset (n,a,ans+1); return;}intMain () {n=3; Print_subset (N,num,0); return 0;}
7.3.3 Binary method
#include <stdio.h>intn =3;voidPrint_subset (intNintans) { for(inti =0; I < n; i + +)//print a subset of {0,1,2,3..n-1} ans if(ans& (1<<i)) printf ("%d", i); printf ("\ n"); return;}intMain () { for(inti =0; I < (1<<N); i + +)//enumerate the encodings corresponding to each subsetPrint_subset (n,i); return 0;}
"Algorithmic Competition Primer Classic" 7.3 Subset Generation "incremental construction" "bit vector Method" "Binary method"