Monday, November 17, 2008

Problem #24

Another permutations problem. Rather simple...one problem was getting it to output, for some reason atoi (str.c_str ()) wasnt working correctly, so I just have it output the string instead.


#include <iostream>
#include <ctime>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
clock_t start = clock();
int total = 0;
int count = 0;
string str;
int holder[] = {0,1,2,3,4,5,6,7,8,9};

sort (holder,holder+10);

do {
count++;
if (count == 1000000)
{
for (unsigned int i = 0; i < 10; i++)
str += holder[i] + '0';
break;
}
} while ( next_permutation (holder,holder+10) );

cout << str << endl
<< "Process took " << (static_cast<double> (clock()) - start) / CLOCKS_PER_SEC << " seconds." << endl;

system("pause");
return 0;
}

No comments: