给定整数N ,任务是找到要从N减去的最小数以获得回文。
例子:
Input: N = 1000
Output: 1
Explanation: Since 1000 – 1 = 999, which is a palindrome, the smallest number to be subtracted is 1.
Input: N = 3456
Output: 13
Explanation: Since 3456 – 13 = 3443, which is a palindrome, the smallest number to be subtracted is 13.
方法:请按照以下步骤解决问题:
- 从N迭代到0 。
- 初始化计数器。在每次迭代中,将N的减少值反向,并将其与N的当前值进行比较。如果两者相等,则打印计数器的值。
- 否则,增加计数器并继续循环,直到N为0为止。
- 打印计数器的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to evaluate minimum
// subtraction required to make
// a number palindrome
void minSub(int N)
{
// Counts number of
// subtractions required
int count = 0;
// Run a loop till N>=0
while (N >= 0) {
// Store the current number
int num = N;
// Store reverse of current number
int rev = 0;
// Reverse the number
while (num != 0) {
int digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
}
// Check if N is palindrome
if (N == rev) {
break;
}
// Increment the counter
count++;
// Reduce the number by 1
N--;
}
// Print the result
cout << count;
}
// Driver Code
int main()
{
int N = 3456;
// Function call
minSub(N);
return 0;
}
Java
// Java program for the
// above approach
import java.util.*;
class GFG{
// Function to evaluate minimum
// subtraction required to make
// a number palindrome
static void minSub(int N)
{
// Counts number of
// subtractions required
int count = 0;
// Run a loop till N>=0
while (N >= 0)
{
// Store the current
// number
int num = N;
// Store reverse of
// current number
int rev = 0;
// Reverse the number
while (num != 0)
{
int digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
}
// Check if N is
// palindrome
if (N == rev)
{
break;
}
// Increment the counter
count++;
// Reduce the number
// by 1
N--;
}
// Print the result
System.out.print(count);
}
// Driver Code
public static void main(String[] args)
{
int N = 3456;
// Function call
minSub(N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to evaluate minimum
# subtraction required to make
# a number palindrome
def minSub(N):
# Counts number of
# subtractions required
count = 0
# Run a loop till N>=0
while (N >= 0):
# Store the current number
num = N
# Store reverse of current number
rev = 0
# Reverse the number
while (num != 0):
digit = num % 10
rev = (rev * 10) + digit
num = num // 10
# Check if N is palindrome
if (N == rev):
break
# Increment the counter
count += 1
# Reduce the number by 1
N -= 1
# Print the result
print(count)
# Driver Code
if __name__ == '__main__':
N = 3456
# Function call
minSub(N)
# This code is contributed by bgangwar59
C#
// C# program for the
// above approach
using System;
class GFG{
// Function to evaluate minimum
// subtraction required to make
// a number palindrome
static void minSub(int N)
{
// Counts number of
// subtractions required
int count = 0;
// Run a loop till N>=0
while (N >= 0)
{
// Store the current
// number
int num = N;
// Store reverse of
// current number
int rev = 0;
// Reverse the number
while (num != 0)
{
int digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
}
// Check if N is
// palindrome
if (N == rev)
{
break;
}
// Increment the counter
count++;
// Reduce the number
// by 1
N--;
}
// Print the result
Console.Write(count);
}
// Driver Code
public static void Main(String[] args)
{
int N = 3456;
// Function call
minSub(N);
}
}
// This code is contributed by gauravrajput1
输出:
13
时间复杂度: O(N * K),其中K是整数的位数。
辅助空间: O(1)