📌  相关文章
📜  将给定数转换为回文数所要减去的最小数

📅  最后修改于: 2021-05-04 16:23:34             🧑  作者: Mango

给定整数N ,任务是找到要从N减去的最小数以获得回文。

例子:

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

  1. N迭代到0
  2. 初始化计数器。在每次迭代中,将N的减少值反向,并将其与N的当前值进行比较。如果两者相等,则打印计数器的值。
  3. 否则,增加计数器并继续循环,直到N0为止。
  4. 打印计数器的值。

下面是上述方法的实现:

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)