fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <math.h>
  4.  
  5. bool is_palindrome(int num)
  6. {
  7. std::vector<int> split_num;
  8. int max_exp = log10(num);
  9. int current_num = num;
  10. for (int e = max_exp; e >= 0; e--)
  11. {
  12. int power = (int)pow(10, e);
  13. split_num.push_back(current_num / power);
  14. current_num = current_num % power;
  15. }
  16. int length = split_num.size();
  17. for (int i = 0; i < length / 2; i++)
  18. if (split_num[i] != split_num[length - 1 - i])
  19. return false;
  20. return true;
  21. }
  22.  
  23. int main() {
  24. int max = 0;
  25. for (int i = 1000; i <= 9999; i++)
  26. for (int j = i; j <= 9999; j++) // wow! such optimization
  27. {
  28. int num = i * j;
  29. if (is_palindrome(num))
  30. if (num > max)
  31. max = num;
  32. }
  33. std::cout << max;
  34. return 0;
  35. }
Time limit exceeded #stdin #stdout 5s 3408KB
stdin
Standard input is empty
stdout
Standard output is empty