📌  相关文章
📜  位数之和为N的最小数字,每个数字最多出现K次

📅  最后修改于: 2021-04-27 20:44:27             🧑  作者: Mango

给定两个正整数NK ,任务是找到数字总和为N的最小数字,并且该数字中的每个不同数字最多出现K次。如果不存在这样的数字,则打印“ -1”

例子:

方法:可以使用贪婪方法解决给定的问题。请按照以下步骤解决问题:

  • 每个数字最多出现K次的任何数字的最大可能数字总和为K * 45
  • 初始化一个字符串,例如res ,以存储形成的结果字符串。
  • 如果N大于K * 45 ,则无法获得该数字。因此,打印“ -1”
  • 否则,请最多从K减去从N91的每个数字。将当前数字添加到res 。继续直到N小于当前数字。
  • 如果N小于当前数字,则将N作为数字插入res
  • 完成上述步骤后,以相反的顺序打印字符串res作为要求的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the smallest number
// whose sum of digits is N and each
// digit occurring at most K times
void findSmallestNumberPossible(int N, int K)
{
    // Maximum sum possible using
    // each digit at most K times
    if (N > 45 * K) {
        cout << "-1";
        return;
    }
 
    // Append smallest number into
    // the resultant string
    string res = "";
    int count = 0;
 
    // Iterate over the range [9, 1]
    for (int i = 9; i >= 1;) {
 
        // If the count of the digit
        // is K, then update count and
        // check for the next digit
        if (count == K) {
            i--;
            count = 0;
        }
 
        // If N is greater than
        // current digit
        if (N > i) {
 
            // Subtract i from N
            N -= i;
 
            // Insert i as a digit
            res += (char)48 + i;
        }
        else {
 
            // Insert remaining N
            // as a digit
            res += (char)N + 48;
            N = 0;
            break;
        }
 
        // Increment count
        count++;
    }
 
    // Reverse the string
    reverse(res.begin(),
            res.end());
 
    // Print the answer
    cout << res;
}
 
// Driver Code
int main()
{
    int N = 25, K = 3;
 
    // Function Call
    findSmallestNumberPossible(N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to find the smallest number
// whose sum of digits is N and each
// digit occurring at most K times
static void findSmallestNumberPossible(int N, int K)
{
     
    // Maximum sum possible using
    // each digit at most K times
    if (N > 45 * K)
    {
        System.out.print("-1");
        return;
    }
 
    // Append smallest number into
    // the resultant string
    StringBuilder res = new StringBuilder();
    int count = 0;
 
    // Iterate over the range [9, 1]
    for(int i = 9; i >= 1😉
    {
         
        // If the count of the digit
        // is K, then update count and
        // check for the next digit
        if (count == K)
        {
            i--;
            count = 0;
        }
 
        // If N is greater than
        // current digit
        if (N > i)
        {
             
            // Subtract i from N
            N -= i;
 
            // Insert i as a digit
            res.append((char)('0' + i));
        }
        else
        {
             
            // Insert remaining N
            // as a digit
            res.append((char)('0' + N));
            N = 0;
            break;
        }
 
        // Increment count
        count++;
    }
 
    // Reverse the string
    res.reverse();
 
    // Print the answer
    System.out.print(res.toString());
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 25, K = 3;
 
    // Function Call
    findSmallestNumberPossible(N, K);
}
}
 
// This code is contributed by jithin


Python3
# Python3 program for the above approach
 
# Function to find the smallest number
# whose sum of digits is N and each
# digit occurring at most K times
def findSmallestNumberPossible(N, K) :
 
    # Maximum sum possible using
    # each digit at most K times
    if (N > 45 * K) :
        print("-1", endl = "");
        return;
 
    # Append smallest number into
    # the resultant string
    res = "";
    count = 0;
 
    # Iterate over the range [9, 1]
    i = 9;   
    while i >= 1 :
 
        # If the count of the digit
        # is K, then update count and
        # check for the next digit
        if (count == K) :
            i -= 1;
            count = 0;
 
        # If N is greater than
        # current digit
        if (N > i) :
 
            # Subtract i from N
            N -= i;
 
            # Insert i as a digit
            res += chr(48 + i);
         
        else :
 
            # Insert remaining N
            # as a digit
            res += chr(N + 48);
            N = 0;
            break;
 
        # Increment count
        count += 1;
 
    # Reverse the string
    res = res[::-1]
 
    # Print the answer
    print(res, end = "");
 
# Driver Code
if __name__ == "__main__" :
 
    N = 25; K = 3;
 
    # Function Call
    findSmallestNumberPossible(N, K);
     
    # This code is contributed by AnkThon


C#
// C# program to implement
// the above approach 
using System;
class GFG
{
  
// Function to find the smallest number
// whose sum of digits is N and each
// digit occurring at most K times
static void findSmallestNumberPossible(int N, int K)
{
      
    // Maximum sum possible using
    // each digit at most K times
    if (N > 45 * K)
    {
        Console.Write("-1");
        return;
    }
  
    // Append smallest number into
    // the resultant string
    string res = "" ;
    int count = 0;
  
    // Iterate over the range [9, 1]
    for (int i = 9; i >= 1;)
    {
  
        // If the count of the digit
        // is K, then update count and
        // check for the next digit
        if (count == K)
        {
            i--;
            count = 0;
        }
  
        // If N is greater than
        // current digit
        if (N > i)
        {
  
            // Subtract i from N
            N -= i;
  
            // Insert i as a digit
            res += (char)('0' + i);
        }
        else
        {
  
            // Insert remaining N
            // as a digit
            res += (char)('0' + N);
            N = 0;
            break;
        }
  
        // Increment count
        count++;
    }
     
     // Reverse the string
     string ans = "";
     for (int i = res.Length - 1; i >= 0; i--)
     {
         ans += res[i] + "";
          
     }
   
     // Print the answer
     Console.WriteLine(ans);
}
  
// Driver Code
public static void Main()
{
    int N = 25, K = 3;
  
    // Function Call
    findSmallestNumberPossible(N, K);
}
}
 
// This code is contributed by susmitakundugoaldanga


输出:
799

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