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
|
#include "stdafx.h"
#include <fstream>
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <time.h>
using std::copy;
using std::cout;
using std::endl;
using std::vector;
using namespace std;
ofstream myfile;
void combinations_r_recursive(const vector<char> &elems, unsigned long req_len,
vector<unsigned long> &pos, unsigned long depth,
unsigned long margin)
{
// Have we selected the number of required elements?
if (depth >= req_len)
{
for (unsigned long ii = 0; ii < pos.size(); ++ii)
myfile << elems[pos[ii]];
myfile << "," << endl;
cout << ". ";
return;
}
// Try to select new elements to the right of the last selected one.
for (unsigned long ii = margin; ii < elems.size(); ++ii)
{
pos[depth] = ii;
combinations_r_recursive(elems, req_len, pos, depth + 1, ii);
}
return;
}
void combinations_r(const vector<char> &elems, unsigned long req_len)
{
assert(req_len > 0 && req_len <= elems.size());
vector<unsigned long> positions(req_len, 0);
combinations_r_recursive(elems, req_len, positions, 0, 0);
}
const unsigned long num_elements = 36;
const unsigned long comb_len = 3;
int main()
{
clock_t start = clock();
myfile.open ("toddkeyfile.csv");
vector<char> elements(num_elements);
char elements_str[num_elements + 1] = "abcdefghijklmnopqrstuvwxyz1234567890";
copy(elements_str, elements_str + num_elements, elements.begin());
combinations_r(elements, comb_len);
myfile.close();
printf("Time Elasped: %f\n",(double)clock() - start);
system("Pause");
return 0;
}
|