📜  最接近N的较小回文数

📅  最后修改于: 2021-04-27 06:27:18             🧑  作者: Mango

给定整数N ,任务是找到小于N的最接近回文数。

例子:

方法:请按照以下步骤解决问题:

  • N不是回文数时,递减N的值
  • 最后,打印发现为回文数的N的更新值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if
// a number is palindrome or not
bool checkPalindrome(int N)
{
    // Stores reverse of N
    int rev = 0;
 
    // Stores the value of N
    int temp = N;
 
    // Calculate reverse of N
    while (N != 0) {
 
        // Update rev
        rev = rev * 10
              + N % 10;
 
        // Update N
        N = N / 10;
    }
 
    // Update N
    N = temp;
 
    // if N is equal to
    // rev of N
    if (N == rev) {
        return true;
    }
 
    return false;
}
 
// Function to find the closest
// smaller palindromic number to N
int closestSmallerPalindrome(int N)
{
 
    // Calculate closest smaller
    // palindromic number to N
    do {
 
        // Update N
        N--;
    } while (N >= 0
             && !checkPalindrome(N));
 
    return N;
}
 
// Driver Code
int main()
{
    int N = 4000;
    cout << closestSmallerPalindrome(N);
    return 0;
}


Java
// Java program to implement
// the above approach
 
import java.util.*;
import java.lang.Math;
 
class GFG {
 
    // Function to check whether
    // a number is palindrome or not
    static boolean checkPalindrome(int N)
    {
        // Stores reverse of N
        int rev = 0;
 
        // Stores the value of N
        int temp = N;
 
        // Calculate reverse of N
        while (N != 0) {
 
            // Update rev
            rev = rev * 10
                  + N % 10;
 
            // Update N
            N = N / 10;
        }
 
        // Update N
        N = temp;
 
        // if N is equal to
        // rev of N
        if (N == rev) {
            return true;
        }
 
        return false;
    }
   
    // Function to find the closest
    // smaller palindromic number to N
    static int
    closestSmallerPalindrome(int N)
    {
 
        // Calculate closest smaller
        // palindromic number to N
        do {
 
            // Update N
            N--;
        } while (N >= 0
                 && !checkPalindrome(N));
 
        return N;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 4000;
        System.out.println(
            closestSmallerPalindrome(N));
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to check if
# a number is palindrome or not
def checkPalindrome(N):
     
    # Stores reverse of N
    rev = 0
 
    # Stores the value of N
    temp = N
 
    # Calculate reverse of N
    while (N != 0):
 
        # Update rev
        rev = rev * 10 + N % 10
 
        # Update N
        N = N // 10
 
    # Update N
    N = temp
 
    # If N is equal to
    # rev of N
    if (N == rev):
        return True
 
    return False
   
# Function to find the closest
# smaller palindromic number to N
def closestSmallerPalindrome(N):
     
    # Calculate closest smaller
    # palindromic number to N
    while N >= 0 and not checkPalindrome(N):
         
        # Update N
        N -= 1
 
    return N
 
# Driver Code
if __name__ == '__main__':
     
    N = 4000
     
    print(closestSmallerPalindrome(N))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to check whether
// a number is palindrome or not
static bool checkPalindrome(int N)
{
     
    // Stores reverse of N
    int rev = 0;
 
    // Stores the value of N
    int temp = N;
 
    // Calculate reverse of N
    while (N != 0)
    {
         
        // Update rev
        rev = rev * 10 + N % 10;
         
        // Update N
        N = N / 10;
    }
     
    // Update N
    N = temp;
 
    // If N is equal to
    // rev of N
    if (N == rev)
    {
        return true;
    }
 
    return false;
}
 
// Function to find the closest
// smaller palindromic number to N
static int closestSmallerPalindrome(int N)
{
     
    // Calculate closest smaller
    // palindromic number to N
    do
    {
         
        // Update N
        N--;
    } while (N >= 0 && !checkPalindrome(N));
 
    return N;
}
 
// Driver Code
public static void Main()
{
    int N = 4000;
     
    Console.WriteLine(closestSmallerPalindrome(N));
}
}
 
// This code is contributed by bgangwar59


输出:
3993








时间复杂度: O(N * log 10 N)
辅助空间: O(log 10 N)