📌  相关文章
📜  查找具有给定数字位数和数字总和的最小数字

📅  最后修改于: 2021-05-06 18:11:25             🧑  作者: Mango

如何找到具有给定的数字总和s和位数d的最小数字?
例子 :

Input  : s = 9, d = 2
Output : 18
There are many other possible numbers 
like 45, 54, 90, etc with sum of digits
as 9 and number of digits as 2. The 
smallest of them is 18.

Input  : s = 20, d = 3
Output : 299

一个简单的解决方案是考虑所有m位数字,并以数字总和为s跟踪最小数字。该解决方案的时间复杂度的上限接近O(10 m )。
有一种贪婪的方法可以解决该问题。这个想法是从最右到最左(或从最低有效数字到最高有效数字)一一填充所有数字。
我们首先从总和s减去1,以便最后有最小的数字。减去1后,我们应用贪婪方法。我们将剩余的总和与9进行比较,如果剩余的总和大于9,则将9放在当前位置,否则我们将剩余的总和。由于我们从右到左填充数字,因此我们将最高的数字放在右侧。以下是该想法的实现。

C++
// C++ program to find the smallest number that can be
// formed from given sum of digits and number of digits.
#include 
using namespace std;
 
// Prints the smallest possible number with digit sum 's'
// and 'm' number of digits.
void findSmallest(int m, int s)
{
    // If sum of digits is 0, then a number is possible
    // only if number of digits is 1.
    if (s == 0)
    {
        (m == 1)? cout << "Smallest number is " << 0
                : cout << "Not possible";
        return ;
    }
 
    // Sum greater than the maximum possible sum.
    if (s > 9*m)
    {
        cout << "Not possible";
        return ;
    }
 
    // Create an array to store digits of result
    int res[m];
 
    // deduct sum by one to account for cases later
    // (There must be 1 left for the most significant
    //  digit)
    s -= 1;
 
    // Fill last m-1 digits (from right to left)
    for (int i=m-1; i>0; i--)
    {
        // If sum is still greater than 9,
        // digit must be 9.
        if (s > 9)
        {
            res[i] = 9;
            s -= 9;
        }
        else
        {
            res[i] = s;
            s = 0;
        }
    }
 
    // Whatever is left should be the most significant
    // digit.
    res[0] = s + 1;  // The initially subtracted 1 is
                     // incorporated here.
 
    cout << "Smallest number is ";
    for (int i=0; i


Java
// Java program to find the smallest number that can be
// formed from given sum of digits and number of digits
 
class GFG
{
    // Function to print the smallest possible number with digit sum 's'
    // and 'm' number of digits
    static void findSmallest(int m, int s)
    {
        // If sum of digits is 0, then a number is possible
        // only if number of digits is 1
        if (s == 0)
        {
            System.out.print(m == 1 ? "Smallest number is 0" : "Not possible");
             
            return ;
        }
  
        // Sum greater than the maximum possible sum
        if (s > 9*m)
        {
            System.out.println("Not possible");
            return ;
        }
  
        // Create an array to store digits of result
        int[] res = new int[m];
  
        // deduct sum by one to account for cases later
        // (There must be 1 left for the most significant
        //  digit)
        s -= 1;
  
        // Fill last m-1 digits (from right to left)
        for (int i=m-1; i>0; i--)
        {
            // If sum is still greater than 9,
            // digit must be 9
            if (s > 9)
            {
                res[i] = 9;
                s -= 9;
            }
            else
            {
                res[i] = s;
                s = 0;
            }
        }
  
        // Whatever is left should be the most significant
        // digit
        res[0] = s + 1;  // The initially subtracted 1 is
                        // incorporated here
  
        System.out.print("Smallest number is ");
        for (int i=0; i


Python3
# Prints the smallest possible
# number with digit sum 's'
# and 'm' number of digits.
 
def findSmallest(m,s):
 
    # If sum of digits is 0,
    # then a number is possible
    # only if number of digits is 1.
    if (s == 0):
         
        if(m == 1) :
              print("Smallest number is 0")
        else :
              print("Not possible")
        return
  
    # Sum greater than the
    # maximum possible sum.
    if (s > 9*m):
     
        print("Not possible")
        return
  
    # Create an array to
    # store digits of result
    res=[0 for i in range(m+1)]
  
    # deduct sum by one to
    # account for cases later
    # (There must be 1 left
    # for the most significant
    #  digit)
    s -= 1
  
    # Fill last m-1 digits
    # (from right to left)
    for i in range(m-1,0,-1):
     
        # If sum is still greater than 9,
        # digit must be 9.
        if (s > 9):
         
            res[i] = 9
            s -= 9
     
        else:
         
            res[i] = s
            s = 0
  
    # Whatever is left should
    # be the most significant
    # digit.
    # The initially subtracted 1 is
    # incorporated here.
    res[0] = s + 1
                    
  
    print("Smallest number is ",end="")
    for i in range(m):
        print(res[i],end="")
 
 
s = 9
m = 2
findSmallest(m, s)
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to find the smallest
// number that can be formed from
// given sum of digits and number
// of digits
using System;
 
class GFG
{
    // Function to print the smallest
    // possible number with digit sum 's'
    // and 'm' number of digits
    static void findSmallest(int m, int s)
    {
        // If sum of digits is 0,
        // then a number is possible
        // only if number of digits is 1
        if (s == 0)
        {
            Console.Write(m == 1 ?
                          "Smallest number is 0" :
                                  "Not possible");
             
            return ;
        }
 
        // Sum greater than the
        // maximum possible sum
        if (s > 9 * m)
        {
            Console.Write("Not possible");
            return ;
        }
 
        // Create an array to
        // store digits of result
        int []res = new int[m];
 
        // deduct sum by one to account
        // for cases later (There must be
        // 1 left for the most significant
        // digit)
        s -= 1;
 
        // Fill last m-1 digits
        // (from right to left)
        for (int i = m - 1; i > 0; i--)
        {
            // If sum is still greater
            // than 9, digit must be 9
            if (s > 9)
            {
                res[i] = 9;
                s -= 9;
            }
            else
            {
                res[i] = s;
                s = 0;
            }
        }
 
        // Whatever is left should be
        // the most significant digit
         
        // The initially subtracted 1 is
        // incorporated here
        res[0] = s + 1;
 
        Console.Write("Smallest number is ");
        for (int i = 0; i < m; i++)
            Console.Write(res[i]);
    }
     
    // Driver Code
    public static void Main ()
    {
        int s = 9, m = 2;
        findSmallest(m, s);
    }
}
 
// This code is contributed by nitin mittal.


PHP
 9 * $m)
    {
        echo "Not possible";
        return ;
    }
 
    // Create an array to store
    // digits of result int res[m];
    // deduct sum by one to account
    // for cases later (There must
    // be 1 left for the most
    // significant digit)
    $s -= 1;
 
    // Fill last m-1 digits
    // (from right to left)
    for ($i = $m - 1; $i > 0; $i--)
    {
        // If sum is still greater
        // than 9, digit must be 9.
        if ($s > 9)
        {
            $res[$i] = 9;
            $s -= 9;
        }
        else
        {
            $res[$i] = $s;
            $s = 0;
        }
    }
 
    // Whatever is left should be
    // the most significant digit.
     
    // The initially subtracted 1
    // is incorporated here.
    $res[0] = $s + 1;
                       
    echo "Smallest number is ";
    for ($i = 0; $i < $m; $i++)
        echo $res[$i];
}
 
// Driver code
$s = 9; $m = 2;
findSmallest($m, $s);
 
// This code is contributed by ajit
?>


Javascript


输出 :

Smallest number is 18