Incremental construction method

Reference

This algorithm is only the output subscript, the actual application should enter another array to store the data//These three ways, in order to understand this took the longest time, too tangled in the details, Rujia write is not particularly clear//These three ways, can be understood as output 0-n-1 of this n number subset// The question is that my sequence is not simply a 0-n-1, if not the case, then what to do,//So, can define an array of data, the three way to directly output the collection is the data array of the Subscript collection//Here it is not difficult to understand the code below the//a array is the subscript array,
Each output is an subscript set #include <iostream> #include <cstdio> #include <cstring> using namespace std; void Print_subset (int n, int *a, int cur) {//cur is the position of the current A array for (int i = 0; i < cur; ++i) {printf ("%d", A[i]
);//If there is an array of data, write Data[a[i]], here no longer repeat} printf ("\ n"); int s = cur?
A[CUR-1] + 1:0; This sentence is more difficult to understand, especially a[cur-1] + 1 here, see for a long time to understand//when cur equals 0, this is the first time to enter the function, so the selection of the set subscript element 0 can be//cur not equal to 0, that is, cur front and subscript elements, in order to get all the Subset, so here can not be missed, then from the smallest choice//That is why a[cur-1]+1 is the smallest subscript, just output the last subscript is a[cur-1], so this one subscript +1, that is not selected the smallest subscript//And then is from the current smallest not yet selected subscript
Start selection as the first element of the next subscript collection//until the subscript position of the n-1, each time the first subscript is selected, the recursive for (int i = s; i < n; ++i) {a[cur] = i;
Print_subset (n, A, cur + 1);
}} int main () {int n; int a[10];//The array holds the number of dataThe subscript of the group, in order to be simple, does not define the data array, the data array is used in the output time (scanf ("%d", &n) = = 1) {Print_subset (n, A, 0);
} return 0;
}

Bit vector method

This algorithm is only the output subscript, the actual application should enter another array to store the data
//This is a good understanding, the equivalent of enumeration, with a bit similar to the binary, the use of arrays, the binary is used to represent the binary position of the number
#include < iostream>
#include <cstring>
#include <cstdio>
using namespace std;
void Print_subset (int n, int *b, int cur) {
if (cur = = N) { //has enumerated a state, output for
(int i = 0; i < n; ++i) {
if (B[i]) {
printf ("%d", I);
}
}
printf ("\ n");
return;
}
B[cur] = 1; The element is
print_subset (n, B, cur + 1) in the next set of enumerations to be enumerated;
B[cur] = 0; The element is not in the next set to be enumerated
print_subset (n, B, cur + 1);
}
int main () {
int n, b[10];
while (CIN >> N) {
print_subset (n, B, 0);
}
return 0;
}

Binary method

This algorithm is only the output subscript, the actual application should enter another array to store the data
//principle: The state is represented by the bits of the number, the binary right-to-left position represents the subscript of the array element
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
void Print_subset (int n, int s) {
//s represents the current binary number representation of the State for
(int i = 0; i < n; ++i) {
if (S & 1 << i) {//1 Right shifts a few to represent the I-bits is 1, the other bit is 0, with the state & operation, if this state contains the number, the output
printf ("%d", I);}
}
printf ("\ n");
}
int main () {
int n;
while (scanf ("%d", &n) = = 1) {
//total n number, so its complete has 2^n binary 1, the corresponding decimal number is 2^n-1
//We have to do is enumerate out each state, 1 right shift n bit, The resulting decimal number is 2^n for
(int i = 0; i < (1 << n); ++i) { //i represents the state of the collection element, which prints out the current collection
Print_subset (n, i);
}
}
return 0;
}