The first method is to continue to add elements, then consider the deletion of one element, a New elements come together
1 void print_subset(int n,int A[],int cur) { 2 for (int i=0;i<cur;i++) 3 printf("%d ",A[i]); 4 printf("\n"); 5 int s = cur ? A[cur-1]+1 : 0; 6 for (int i=s;i<n;i++) { 7 A[cur] = i; 8 print_subset(n,A,cur+1); 9 }
The second method is better understood, that is, for each element we have two cases (take and not take)
1 void print_Subset(int n,int B[],int cur) { 2 if (cur == n) { 3 for (int i=0;i<cur;i++) { 4 if (B[i]) 5 printf("%d ",i); 6 } 7 printf("\n"); 8 return ; 9 } 10 B[cur] = 1; 11 print_Subset(n,B,cur+1); 12 B[cur] = 0; 13 print_Subset(n,B,cur+1); 14 }
The third method Binary!
Binary method is very simple, in fact, the binary 0 and 1 as a "switch." 1 for selecting, 0 is not selected.
In fact, every binary actually corresponds to the subject of our next map array (My personal understanding is this)
Seek length n and is continuous from a subset of the array of 0 ~ n-1
1 void print_subset(int n,int s) { 2 for (int i=0;i<n;i++) { 3 if (s & (1<<i)) 4 printf("%d ",i); 5 } 6 printf("\n"); 7 }
A subset of the array seek any length of n if understand I said earlier, is the subject of the next binary array of maps, this is very simple
1 int a[3] = {5, 4, 6}; 2 3 void print_subset(int n,int s) { 4 for (int i=0;i<n;i++) { 5 if (s & (1<<i)) 6 printf("%d ",a[i]); 7 } 8 printf("\n"); 9 }