如何找到给定位数和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 )。
有一个贪婪的方法来解决这个问题。这个想法是从最右边到最左边(或从最低有效数字到最高有效数字)一一填充所有数字。
我们最初从 sum 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
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。