Advertisement
eXFq7GJ1cC

Untitled

Aug 29th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. // Find the largest palindrome made from the product of two 3-digit numbers.
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <cstring>
  6. #include <boost/lexical_cast.hpp>
  7. using namespace std;
  8.  
  9. string convert_double(double num)
  10. {
  11.     string str = boost::lexical_cast<string>(num);
  12.     return str;
  13. }
  14.  
  15. bool checkPalindrome(string str)
  16. {
  17.     int size = str.length();
  18.     for (int i = 0; i < size / 2; i++)
  19.         if (str[i] != str[size - 1 - i])
  20.             return false;
  21.     return true;
  22. }
  23.  
  24. int main()
  25. {
  26.     double num = 0;
  27.  
  28.     for (int i = 999; i > 99; i--) {
  29.         for (int j = 999; j > 99; j--) {
  30.             num = i * j;
  31.             if (checkPalindrome(convert_double(num))) {
  32.                 cout << num;
  33.                 return 0;
  34.             }
  35.         }
  36.     }
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement