fork download
  1. using static System.Console;
  2. using System.Linq;
  3. using System.Numerics;
  4. using System.Text.RegularExpressions;
  5.  
  6. public static class Extension
  7. {
  8. public static string rev(this string s)
  9. {
  10. return new string(s.Reverse().ToArray());
  11. }
  12. }
  13.  
  14. class Program
  15. {
  16. static BigInteger[] NearestPalindromeNumbers(BigInteger n)
  17. {
  18. string s = n.ToString();
  19. if (new Regex("^10+$").Match(s).Success) return new[] {n - 1, n + 1};
  20.  
  21. int j = s.Length / 2;
  22. int i = s.Length - j;
  23. string a = s.Substring(0, i);
  24. string b = a.Substring(0, j).rev();
  25. var x = BigInteger.Parse(a + b);
  26. if (x == n) return new[] {x};
  27.  
  28. a = (BigInteger.Parse(a) + (x < n ? 1 : -1)).ToString();
  29. b = a.Substring(0, j).rev();
  30. var y = BigInteger.Parse(a + b);
  31. var d = BigInteger.Abs(x - n) - BigInteger.Abs(y - n);
  32. if (d < 0) return new[] {x};
  33. if (d > 0) return new[] {y};
  34. return x < y ? new[] {x, y} : new[] {y, x};
  35. }
  36.  
  37. static BigInteger[] q = {0, 5, 17, 100, 2024, 12345679042654321, BigInteger.Parse("31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989")};
  38.  
  39. static void Main()
  40. {
  41. foreach (var n in q) WriteLine($"[{string.Join(", ", NearestPalindromeNumbers(n))}]");
  42. }
  43. }
Success #stdin #stdout 0.1s 32728KB
stdin
Standard input is empty
stdout
[0]
[5]
[22]
[99, 101]
[2002]
[12345678987654321, 12345679097654321]
[31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491319491103818397221984272575881537659472699732644708458115013971162391837116812903591959575630727503349061151491596414831256640288450350331100630952987634635171904529282690290251884718855136066007854273721419420627063933128466234540168430643296658465419091021725613876873328465748216443395665790188244691830394598492264469555011258391072014820547111848218049535271322850559064483907466032823156808412897607112435284308268998026826046187032954494790285015739939617914882059723833462648323979853562951413]