(1) incremental Constructor
# Include
# Include using namespace std; const int MAXN = 1000; int A [MAXN], n; void print_subset (int n, int * A, int cur) {for (int I = 0; I <cur; ++ I) cout <A [I] <"; cout <endl; int s = (cur? A [cur-1] + 1: 0); // select the minimum number that can be filled in at the cur position! For (int I = s; I <n; ++ I) {A [cur] = I; print_subset (n, A, cur + 1) ;}} int main () {cin> n; print_subset (n, A, 0); return 0 ;}
(2) bit vector method
# Include
# Include using namespace std; const int MAXN = 1000; int B [MAXN], n; void print_subset (int n, int * B, int cur) {if (cur = n) {for (int I = 0; I <cur; ++ I) {if (B [I]) cout <I <";}cout <endl; return;} B [cur] = 1; // select the element print_subset (n, B, cur + 1); B [cur] = 0; // do not select the cur element print_subset (n, B, cur + 1);} int main () {cin> n; print_subset (n, B, 0); return 0 ;}
It must be a complete subset after "whether all elements are selected" is determined.