fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <regex>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. vector<string> NearestPalindromeNumbers(string &s)
  10. {
  11. string x(s), y(s), z(s);
  12.  
  13. int k = s.length();
  14. int j = k / 2;
  15. int i = k - j;
  16.  
  17. if (regex_match(s, regex("^10+$"))) return {string(k - 1, '9'), (y[k - 1]++, y)};
  18.  
  19. reverse_copy(&x[0], &x[j], &x[i]);
  20. if (x == s) return {x};
  21.  
  22. bool ascending = x < s;
  23. char r = 1;
  24. if (ascending) {
  25. for_each(y.rend() - i, y.rend(), [&r](char &y) {if (r) if (r = ++y > '9') y = '0';});
  26. } else {
  27. for_each(y.rend() - i, y.rend(), [&r](char &y) {if (r) if (r = --y < '0') y = '9';});
  28. }
  29. reverse_copy(&y[0], &y[j], &y[i]);
  30.  
  31. transform(x.rbegin(), x.rend(), y.rbegin(), z.rbegin(), [&r](char &x, char &y) {
  32. char z = x + y + r;
  33. return (r = z > '0' + '9') ? z -= 10 : z;
  34. });
  35. for (char &z : z) r = (z += 10 * r) % 2, z /= 2;
  36.  
  37. string &a = ascending ? x : y, &b = ascending ? y : x;
  38.  
  39. int d = strcmp(&s[0], &z[0]);
  40. if (!r && !d) return {a, b};
  41. return {d <= 0 ? a : b};
  42. }
  43.  
  44. string q[] = {"0", "5", "17", "100", "2024", "12345679042654321", "31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989"};
  45.  
  46. int main(void)
  47. {
  48. for (auto &s : q) {
  49. auto v = NearestPalindromeNumbers(s);
  50. for (auto &x : v) cout << (&x == &v[0] ? "[" : ", ") << x;
  51. cout << "]" << endl;
  52. }
  53. return 0;
  54. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
[0]
[5]
[22]
[99, 101]
[2002]
[12345678987654321, 12345679097654321]
[31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491319491103818397221984272575881537659472699732644708458115013971162391837116812903591959575630727503349061151491596414831256640288450350331100630952987634635171904529282690290251884718855136066007854273721419420627063933128466234540168430643296658465419091021725613876873328465748216443395665790188244691830394598492264469555011258391072014820547111848218049535271322850559064483907466032823156808412897607112435284308268998026826046187032954494790285015739939617914882059723833462648323979853562951413]