📜  找到具有给定位数和位数总和的最大数

📅  最后修改于: 2021-10-26 06:53:28             🧑  作者: Mango

如何找到给定位数和s和位数d的最大数?
例子:

Input  : s = 9, d = 2
Output : 90

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

一个简单的解决方案是考虑所有 m 位数字并跟踪最大数字,数字总和为 s。该解决方案的时间复杂度的上界是 O(10 m )。
有一个贪婪的方法来解决这个问题。这个想法是从最左边到最右边(或从最重要的数字到最不重要的数字)一一填充所有数字。
我们将剩余和与 9 进行比较,如果剩余和大于 9,我们将 9 放在当前位置,否则我们将剩余的和。由于我们从左到右填充数字,我们将最高的数字放在左侧,因此得到最大的数字。
下图是上述方法的说明:

下面是上述方法的实现:

C++
// C++ program to find the largest number that can be
// formed from given sum of digits and number of digits.
#include 
using namespace std;
 
// Prints the smalles possible number with digit sum 's'
// and 'm' number of digits.
void findLargest(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 << "Largest 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];
 
    // Fill from most significant digit to least
    // significant digit.
    for (int i=0; i= 9)
        {
            res[i] = 9;
            s -= 9;
        }
 
        // If remaining sum becomes less than 9, then
        // fill the remaining sum
        else
        {
            res[i] = s;
            s = 0;
        }
    }
 
    cout << "Largest number is ";
    for (int i=0; i


Java
// Java program to find the largest number that can be
// formed from given sum of digits and number of digits
 
class GFG
{
    // Function to print the largest possible number with digit sum 's'
    // and 'm' number of digits
    static void findLargest(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 ? "Largest 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];
  
        // Fill from most significant digit to least
        // significant digit
        for (int i=0; i= 9)
            {
                res[i] = 9;
                s -= 9;
            }
  
            // If remaining sum becomes less than 9, then
            // fill the remaining sum
            else
            {
                res[i] = s;
                s = 0;
            }
        }
  
        System.out.print("Largest number is ");
        for (int i=0; i


Python3
# Python 3 program to find
# the largest number that
# can be formed from given
# sum of digits and number
# of digits.
 
 
# Prints the smalles
# possible number with digit
# sum 's' and 'm' number of
# digits.
def findLargest( 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("Largest number is " , "0",end = "")
        else :
            print("Not possible",end = "")
     
        return
 
    # Sum greater than the
    # maximum possible sum.
    if (s > 9 * m) :
        print("Not possible",end = "")
        return
     
    # Create an array to
    # store digits of
    # result
    res = [0] * m
 
    # Fill from most significant
    # digit to least significant
    # digit.
    for i in range(0, m) :
         
        # Fill 9 first to make
        # the number largest
        if (s >= 9) :
            res[i] = 9
            s = s - 9
         
        # If remaining sum
        # becomes less than
        # 9, then fill the
        # remaining sum
        else :
            res[i] = s
            s = 0
         
         
    print( "Largest number is ",end = "")
     
    for i in range(0, m) :
        print(res[i],end = "")
 
# Driver code
s = 9
m = 2
findLargest(m, s)
 
# This code is contributed by Nikita Tiwari.


C#
// C# program to find the
// largest number that can
// be formed from given sum
// of digits and number of digits
using System;
 
class GFG
{
     
    // Function to print the 
    // largest possible number
    // with digit sum 's' and
    // 'm' number of digits
    static void findLargest(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 ?
                   "Largest number is 0" :
                          "Not possible");
             
            return ;
        }
 
        // Sum greater than the
        // maximum possible sum
        if (s > 9 * m)
        {
            Console.WriteLine("Not possible");
            return ;
        }
 
        // Create an array to
        // store digits of result
        int []res = new int[m];
 
        // Fill from most significant
        // digit to least significant digit
        for (int i = 0; i < m; i++)
        {
            // Fill 9 first to make
            // the number largest
            if (s >= 9)
            {
                res[i] = 9;
                s -= 9;
            }
 
            // If remaining sum becomes
            // less than 9, then
            // fill the remaining sum
            else
            {
                res[i] = s;
                s = 0;
            }
        }
 
        Console.Write("Largest number is ");
        for (int i = 0; i < m; i++)
            Console.Write(res[i]);
    }
     
    // Driver Code
    static public void Main ()
    {
        int s = 9, m = 2;
        findLargest(m, s);
    }
}
 
// This code is Contributed by ajit


PHP
 9 * $m)
    {
        echo "Not possible";
        return ;
    }
 
    // Create an array to store
    // digits of result Fill from
    // most significant digit to
    // least significant digit.
    for ($i = 0; $i < $m; $i++)
    {
        // Fill 9 first to make
        // the number largest
        if ($s >= 9)
        {
            $res[$i] = 9;
            $s -= 9;
        }
 
        // If remaining sum becomes
        // less than 9, then fill
        // the remaining sum
        else
        {
            $res[$i] = $s;
            $s = 0;
        }
    }
 
    echo "Largest number is ";
    for ($i = 0; $i < $m; $i++)
        echo $res[$i];
}
 
// Driver code
$s = 9; $m = 2;
findLargest($m, $s);
 
// This code is contributed by m_kit
?>


Javascript


输出 :

Largest number is 90