📜  找到第 K 个最小的奇数回文数

📅  最后修改于: 2022-05-13 01:56:09.715000             🧑  作者: Mango

找到第 K 个最小的奇数回文数

给定一个正整数K ,任务是找到第 K最小的奇数回文数。

例子:

方法:可以根据以下观察解决给定的问题:

  • 第一个长度为 1 的回文数是 1、2、3、4、5、6、7、8 和 9。
  • 第一个长度为 3 的回文数是 101,这是第 10 个最小的奇数回文数。类似地,第 11 、第 12 、第 13 、...、第 99 个最小回文分别为 111、121、131 ...、999。
  • 因此,第K最小的奇数回文数可以由KK的倒数组成,除了最后一位。

根据上述观察,第K个最小的奇数回文数是通过在K的末尾附加除最后一位之外的所有数字的反转来给出的。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the Kth smallest
// odd length palindrome
int oddLengthPalindrome(int k)
{
     
    // Store the original number K
    int palin = k;
 
    // Removing the last digit of K
    k = k / 10;
 
    // Generate the palindrome by
    // appending the reverse of K
    // except last digit to itself
    while (k > 0)
    {
         
        // Find the remainder
        int rev = k % 10;
 
        // Add the digit to palin
        palin = (palin * 10) + rev;
 
        // Divide K by 10
        k = k / 10;
    }
 
    // Return the resultant palindromic
    // number formed
    return palin;
}
 
// Driver Code
int main()
{
    int k = 504;
 
    cout << oddLengthPalindrome(k);
}
 
// This code is contributed by rishavmahato348


Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
 
class GFG{
     
// Function to find the Kth smallest
// odd length palindrome
static int oddLengthPalindrome(int k)
{
     
    // Store the original number K
    int palin = k;
     
    // Removing the last digit of K
    k = k / 10;
     
    // Generate the palindrome by
    // appending the reverse of K
    // except last digit to itself
    while (k > 0)
    { 
         
        // Find the remainder
        int rev = k % 10;
         
        // Add the digit to palin
        palin = (palin * 10) + rev;
         
        // Divide K by 10
        k = k / 10;
    }
     
    // Return the resultant palindromic
    // number formed
    return palin;
}
 
// Driver Code
public static void main(String[] args)
{
    int k = 504;
     
    System.out.println(oddLengthPalindrome(k));
}
}
 
// This code is contributed by Sudhanshu Bhagat & Govind Choudhary


Python3
# Python3 program for the above approach
 
# Function to find the Kth smallest
# odd length palindrome number
def oddLengthPalindrome(K):
 
    # Store the original number K
    palin = K
 
    # Removing the last digit of K
    K = K // 10
 
    # Generate the palindrome by
    # appending the reverse of K
    # except last digit to itself
    while (K > 0):
       
        # Find the remainder
        rev = K % 10
         
        # Add the digit to palin
        palin = palin * 10 + rev
         
        # Divide K by 10
        K = K // 10
 
    # Return the resultant palindromic
    # number formed
    return palin
 
# Driver Code
if __name__ == '__main__':
 
    K = 504
    print(oddLengthPalindrome(K))
     
     
#Contributed by Govind Choudhary & Pallav Pushparaj


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the Kth smallest
// palindrome of odd length
static int oddLengthPalindrome(int k)
{
     
    // Store the original number K
    int palin = k;
     
    // Removing the last digit of K
    k = k / 10;
     
    // Generate the palindrome by
    // appending the reverse of K
    // except last digit to itself
    while (k > 0)
    {
         
        // Find the remainder
        int rev = k % 10;
         
        // Add the digit to palin
        palin = (palin * 10) + rev;
         
        // Divide K by 10
        k = k / 10;
    }
     
    // Return the resultant palindromic
    // number formed
    return palin;
}
 
// Driver Code
static void Main(string[] args)
{
    int k = 504;
 
    Console.WriteLine(oddLengthPalindrome(k));
}
}
 
// This code is contributed by Sudhanshu Bhagat & Govind Choudhary


Javascript


输出:
50405

时间复杂度: O(log 10 K)
辅助空间: O(1)