Sunday, November 16, 2008

Problem #4


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

using namespace std;

bool isPalindromic (int);
string reverseString (string);

int main()
{
clock_t start = clock();
int total = 0;

for (unsigned int i = 100; i < 1000; i++)
for (unsigned int j = 100; j < 1000; j++)
if (isPalindromic (i * j))
if (i * j > total)
total = i * j;

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

system ("pause");
return 0;
}

bool isPalindromic (int num)
{
char buffer[32];
string str;

sprintf (buffer, "%i", num);
str = buffer;

if (str == reverseString (str))
return true;
return false;
}

string reverseString (string str)
{
string str2;

for (unsigned int i = 0; i < str.size(); i++)
str2 += str[str.size () - i - 1];
return str2;
}

No comments: