0004.c 903 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. bool ispalindrome(const char *cstr, uint64_t cstr_size);
  5. int
  6. main(int argc, const char **argv)
  7. {
  8. uint64_t res = 0;
  9. uint64_t sum = 0;
  10. uint64_t upper_limit = 999;
  11. uint64_t n1 = upper_limit, n2 = upper_limit;
  12. char str[16] = {0};
  13. uint64_t str_size = 0;
  14. while ( n2 > 0 ) {
  15. sum = n1 * n2;
  16. str_size = (uint64_t)sprintf(str, "%ld", sum);
  17. if ( ispalindrome(str, str_size) ) {
  18. if ( sum > res ) {
  19. res = sum;
  20. }
  21. }
  22. n1 -= ( n1 > 0 );
  23. if ( n1 == 0 ) {
  24. n1 = upper_limit;
  25. --n2;
  26. }
  27. }
  28. printf("Result = %ld!\n", res);
  29. (void) argc; (void) argv;
  30. return 0;
  31. }
  32. bool
  33. ispalindrome(const char *cstr, uint64_t cstr_size)
  34. {
  35. uint64_t ptr_start = 0;
  36. uint64_t ptr_end = cstr_size-1;
  37. while ( ptr_start < ptr_end ) {
  38. if ( cstr[ptr_start] != cstr[ptr_end] ) {
  39. return false;
  40. }
  41. ++ptr_start;
  42. --ptr_end;
  43. }
  44. return true;
  45. }