monito2207

d2_2

Nov 18th, 2021
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 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.     return false;
  17. }
  18.  
  19. bool NearestPalindrom(int PalL, int PalR, int Num) {
  20.     if (PalL - Num > Num - PalR)
  21.     {
  22.         return true;
  23.     }
  24.     return false;
  25. }
  26.  
  27. int main() {
  28.     long long int Num;
  29.     int n_left, n_right;
  30.     cin >> Num;
  31.     if (Num >= 1)
  32.     {
  33.         if (Num < 101)
  34.         {
  35.             cout << "101" << " ";
  36.         }
  37.         else
  38.         {
  39.             if (PalindromCheck(Num) == true)
  40.             {
  41.                 cout << Num;
  42.             }
  43.             else
  44.             {
  45.                 for (int i = Num;; i++)
  46.                 {
  47.                     if (PalindromCheck(i) == true)
  48.                     {
  49.                         n_right = i;
  50.                         break;
  51.                     }
  52.                 }
  53.                 for (int i = Num ;; i--)
  54.                 {
  55.                     n_left = i;
  56.                     break;
  57.                 }
  58.                 if (NearestPalindrom(n_left, n_right, Num) == false)
  59.                 {
  60.                     cout << n_left << " ";
  61.                 }
  62.                 else
  63.                 {
  64.                     cout << n_right << " ";
  65.                 }
  66.             }
  67.         }
  68.     }
  69.     else
  70.     {
  71.         cout << "-1" << " ";
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment