Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- bool PalindromCheck(int Num) {
- long long int reversedNum = 0;
- int lastDigit;
- while (Num) {
- lastDigit = Num % 10;
- reversedNum = (reversedNum * 10) + lastDigit;
- Num /= 10;
- }
- if (reversedNum == Num)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool NearestPalindrom(int PalL, int PalR, int Num) {
- if (Num - PalL > PalR - Num)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- int main() {
- long long int Num;
- int PalR, PalL;
- cin >> Num;
- if (Num >= 1)
- {
- if (Num < 101)
- {
- cout << "101" << " ";
- }
- else
- {
- if (PalindromCheck(Num) == true)
- {
- cout << Num;
- }
- else
- {
- for (int i = Num;;)
- {
- i++;
- if (PalindromCheck(i) == true)
- {
- PalR = i;
- break;
- }
- }
- for (int i = Num;;)
- {
- i--;
- if (PalindromCheck(i) == true)
- {
- PalL = i;
- break;
- }
- }
- if (NearestPalindrom(PalL, PalR, Num) == false)
- {
- cout << PalL << " ";
- }
- else
- {
- cout << PalR << " ";
- }
- }
- }
- }
- else
- {
- cout << "-1" << " ";
- }
- return main();
- }
Advertisement
Add Comment
Please, Sign In to add comment