fork download
  1. #include <stdio.h>
  2. unsigned rev(unsigned n) {
  3. unsigned acc;
  4. for (acc = 0; n; acc = acc * 10 + n % 10, n /= 10);
  5. return acc;
  6. }
  7. struct found {unsigned count, first, second, error;};
  8. struct found f(unsigned n) {
  9. unsigned a, b;
  10. if (n == rev(n)) return (struct found){1, n};
  11. for (a = n - 1, b = n + 1; ;a--, b++)
  12. switch ((a == rev(a)) * 10 + (b == rev(b))) {
  13. case 11: return (struct found){2, a, b};
  14. case 10: return (struct found){1, a};
  15. case 01: return (struct found){1, b};
  16. case 00: break;
  17. default: return (struct found){0, -1, -1, 1};
  18. }
  19. return (struct found){0, -1, -1, 2};
  20. }
  21. void g(unsigned n) {
  22. struct found found = f(n);
  23. printf("入力: %u\n", n);
  24. switch (found.count) {
  25. case 1: printf("出力: [%u]\n\n", found.first);break;
  26. case 2: printf("出力: [%u, %u]\n\n", found.first, found.second);break;
  27. default: printf("出力: []\n\n");
  28. }
  29. }
  30. int main() {
  31. g(0), g(17), g(100);
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
入力: 0
出力: [0]

入力: 17
出力: [22]

入力: 100
出力: [99, 101]