1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <iostream>
#include <fstream>
using namespace std;
ofstream dt("records.txt"); // result
const int SIZE = 19; // size 18 still works
void output(char *A, bool *B) {
for(int i = 0; i < SIZE; ++i) {
if(B[i]) {
dt << A[i] <<" ";
}
}
dt << endl;
}
void recur(char *A, bool *B, int a, int b, int x) {
if(a < x) {
output(A, B);
B[a] = false;
if(a + 1 < x) {
B[a + 1] = true;
recur(A, B, a + 1, b, x);
} else {
int i = a - 1, j = 1;
for(; i >= 0; --i) {
if(B[i]) {
B[i] = false;
++j;
} else {
break;
}
}
if(i >= 0) {
for(; i >= 0; --i) {
if(B[i]) {
break;
}
}
if(i >= 0) {
B[i] = false;
++j;
for(++i; i < x; ++i) {
B[i] = true;
--j;
if(!j) {
break;
}
}
if(i < x) {
recur(A, B, i, b, x);
}
}
}
}
}
}
void run2(char *A, bool *B, int x) {
B[x] = true;
output(A, B);
for(int i = 0; i < x; ++i) {
for(int j = 0; j <= i; ++j) {
B[j] = true;
}
recur(A, B, i, i + 1, x);
for(int j = 0; j <= i; ++j) {
B[j] = false;
}
}
B[x] = false;
}
void run(char *A, bool *B) {
for(int i = 0; i < SIZE; ++i) {
run2(A, B, i);
}
}
int main() {
bool B[SIZE]; // mark the item
char A[SIZE]; // A to Z
for(int i = 0; i < SIZE; ++i) {
A[i] = (char)(i + 'A');
B[i] = false;
}
run(A, B); //combination
cout << "done" << endl;
system("pause");
return 0;
}
|