📜  将 N 表示为总和所需的最少回文数 |设置 1

📅  最后修改于: 2021-09-22 09:48:13             🧑  作者: Mango

给定一个数字 N,我们必须找到将 N 表示为它们的总和所需的最小回文数。

例子:

方法:
我们可以使用动态规划来解决这个问题。这个想法是首先以排序的方式生成最多 N 的所有回文,然后我们可以将此问题视为子集和问题的变体,我们必须找到最小子集的大小,使其总和为 N .

下面是上述方法的实现:

CPP
// C++ implementation of above approach
#include 
using namespace std;
  
// Declaring the DP table as global variable
vector > dp;
  
  
// A utility for creating palindrome
int createPalindrome(int input, bool isOdd)
{
    int n = input;
    int palin = input;
  
    // checks if number of digits is odd or even
    // if odd then neglect the last digit of input in
    // finding reverse as in case of odd number of
    // digits middle element occur once
    if (isOdd)
        n /= 10;
  
    // Creates palindrome by just appending revers
    // of number to itself
    while (n > 0) {
        palin = palin * 10 + (n % 10);
        n /= 10;
    }
  
    return palin;
}
  
// Function to generate palindromes
vector generatePalindromes(int N)
{
    vector palindromes;
    int number;
  
    // Run two times for odd and even length palindromes
    for (int j = 0; j < 2; j++) {
        // Creates palindrome numbers with first half as i.
        // Value of j decides whether we need an odd length
        // or even length palindrome.
        int i = 1;
        while ((number = createPalindrome(i++, j)) <= N)
            palindromes.push_back(number);
    }
  
    return palindromes;
}
  
// Function to find the minimum
// number of elements in a sorted
// array A[i..j] such that their sum is N
long long minimumSubsetSize(vector& A, int i, int j, int N)
{
    if (!N)
        return 0;
  
    if (i > j || A[i] > N)
        return INT_MAX;
  
    if (dp[i][N])
        return dp[i][N];
  
    dp[i][N] = min(1 + minimumSubsetSize(A, i + 1, j,
                                         N - A[i]),
                   minimumSubsetSize(A, i + 1, j, N));
  
    return dp[i][N];
}
  
// Function to find the minimum
// number of palindromes that N
// can be expressed as a sum of
int minimumNoOfPalindromes(int N)
{
    // Getting the list of all palindromes upto N
    vector palindromes = generatePalindromes(N);
  
    // Sorting the list of palindromes
    sort(palindromes.begin(), palindromes.end());
  
    // Initializing the DP table
    dp = vector >(palindromes.size(),
                                    vector(N + 1, 0));
  
    // Returning the required value
    return minimumSubsetSize(palindromes, 0,
                             palindromes.size() - 1, N);
}
  
// Driver code
int main()
{
    int N = 65;
    cout << minimumNoOfPalindromes(N);
    return 0;
}


Python3
# Python3 implementation of above approach
  
# Declaring the DP table as global variable
dp = [[0 for i in range(1000)] for i in range(1000)]
  
# A utility for creating palindrome
def createPalindrome(input, isOdd):
  
    n = input
    palin = input
  
    # checks if number of digits is odd or even
    # if odd then neglect the last digit of input in
    # finding reverse as in case of odd number of
    # digits middle element occur once
    if (isOdd):
        n //= 10
  
    # Creates palindrome by just appending revers
    # of number to itself
    while (n > 0):
        palin = palin * 10 + (n % 10)
        n //= 10
  
    return palin
  
# Function to generate palindromes
def generatePalindromes(N):
  
    palindromes = []
    number = 0
  
    # Run two times for odd and even length palindromes
    for j in range(2):
          
        # Creates palindrome numbers with first half as i.
        # Value of j decides whether we need an odd length
        # or even length palindrome.
        i = 1
        number = createPalindrome(i, j)
        while number <= N:
            number = createPalindrome(i, j)
            palindromes.append(number)
            i += 1
  
    return palindromes
  
# Function to find the minimum
# number of elements in a sorted
# array A[i..j] such that their sum is N
def minimumSubsetSize(A, i, j, N):
  
    if (not N):
        return 0
  
    if (i > j or A[i] > N):
        return 10**9
  
    if (dp[i][N]):
        return dp[i][N]
  
    dp[i][N] = min(1 + minimumSubsetSize(A, i + 1, j, N - A[i]), 
                    minimumSubsetSize(A, i + 1, j, N))
  
    return dp[i][N]
  
# Function to find the minimum
# number of palindromes that N
# can be expressed as a sum of
def minimumNoOfPalindromes(N):
  
    # Getting the list of all palindromes upto N
    palindromes = generatePalindromes(N)
  
    # Sorting the list of palindromes
    palindromes = sorted(palindromes)
  
    # Returning the required value
    return minimumSubsetSize(palindromes, 0, len(palindromes) - 1, N)
  
# Driver code
N = 65
print(minimumNoOfPalindromes(N))
  
# This code is contributed by mohit kumar 29


输出:
3

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。