monito2207

d2_2.2

Nov 20th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool PalindromCheck(int Num) {
  5.     long long int reversedNum = 0;
  6.     int lastDigit;
  7.     while (Num) {
  8.         lastDigit = Num % 10;
  9.         reversedNum = (reversedNum * 10) + lastDigit;
  10.         Num /= 10;
  11.     }
  12.     if (reversedNum == Num)
  13.     {
  14.         return true;
  15.     }
  16.     else
  17.     {
  18.         return false;
  19.     }
  20. }
  21.  
  22. bool NearestPalindrom(int PalL, int PalR, int Num) {
  23.     if (Num - PalL > PalR - Num)
  24.     {
  25.         return true;
  26.     }
  27.     else
  28.     {
  29.         return false;
  30.     }
  31. }
  32.  
  33. int main() {
  34.     long long int Num;
  35.     int PalR, PalL;
  36.     cin >> Num;
  37.     if (Num >= 1)
  38.     {
  39.         if (Num < 101)
  40.         {
  41.             cout << "101" << " ";
  42.         }
  43.         else
  44.         {
  45.             if (PalindromCheck(Num) == true)
  46.             {
  47.                 cout << Num;
  48.             }
  49.             else
  50.             {
  51.                 for (int i = Num;;)
  52.                 {
  53.                     i++;
  54.                     if (PalindromCheck(i) == true)
  55.                     {
  56.                         PalR = i;
  57.                         break;
  58.                     }
  59.                 }
  60.                 for (int i = Num;;)
  61.                 {
  62.                     i--;
  63.                     if (PalindromCheck(i) == true)
  64.                     {
  65.                         PalL = i;
  66.                         break;
  67.                     }
  68.                 }
  69.                 if (NearestPalindrom(PalL, PalR, Num) == false)
  70.                 {
  71.                     cout << PalL << " ";
  72.                 }
  73.                 else
  74.                 {
  75.                     cout << PalR << " ";
  76.                 }
  77.             }
  78.         }
  79.     }
  80.     else
  81.     {
  82.         cout << "-1" << " ";
  83.     }
  84.     return main();
  85. }
Advertisement
Add Comment
Please, Sign In to add comment