给定整数N ,任务是找到小于N的最接近回文数。
例子:
Input: N = 4000
Output: 393
Explanation:
3993 is the closest palindromic number to N( = 4000) which is also smaller than N( = 4000). Therefore, 3993 is the required answer.
Input: N = 2889
Output: 2882
方法:请按照以下步骤解决问题:
- 当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)