给定两个整数S和D ,任务是找到具有D位数的数字及其位数之和作为S ,以使数字中最大位数和最小位数之差尽可能小。如果可以使用多个这样的数字,请打印最小的数字。
例子:
Input: S = 25, D = 4
Output: 6667
The difference between maximum digit 7 and minimum digit 6 is 1.
Input: S = 27, D = 3
Output: 999
方法:
- 本文已经讨论了查找给定位数和总和的最小数。
- 在本文中,其想法是最小化所需数字中最大和最小数字之间的差异。因此,和s应该均匀地分布在d位数字之间。
- 如果总和是均匀分布的,则差异最多为1。当总和s可被d整除时,差异为零。在这种情况下,每个数字都具有等于s / d的相同值。
- 当和s不能被d整除时,差为1。在这种情况下,在为每个数字分配了值s / d之后,仍然保留s%d个总和值进行分配。
- 由于需要最小的数字,因此该剩余值在数字的最后s%d位之间平均分配,即,数字中的最后s%d位增加1。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to find the number having
// sum of digits as s and d number of
// digits such that the difference between
// the maximum and the minimum digit
// the minimum possible
string findNumber(int s, int d)
{
// To store the final number
string num = "";
// To store the value that is evenly
// distributed among all the digits
int val = s / d;
// To store the remaining sum that still
// remains to be distributed among d digits
int rem = s % d;
int i;
// rem stores the value that still remains
// to be distributed
// To keep the difference of digits minimum
// last rem digits are incremented by 1
for (i = 1; i <= d - rem; i++) {
num = num + to_string(val);
}
// In the last rem digits one is added to
// the value obtained by equal distribution
if (rem) {
val++;
for (i = d - rem + 1; i <= d; i++) {
num = num + to_string(val);
}
}
return num;
}
// Driver function
int main()
{
int s = 25, d = 4;
cout << findNumber(s, d);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to find the number having
// sum of digits as s and d number of
// digits such that the difference between
// the maximum and the minimum digit
// the minimum possible
static String findNumber(int s, int d)
{
// To store the final number
String num = "";
// To store the value that is evenly
// distributed among all the digits
int val = s / d;
// To store the remaining sum that still
// remains to be distributed among d digits
int rem = s % d;
int i;
// rem stores the value that still remains
// to be distributed
// To keep the difference of digits minimum
// last rem digits are incremented by 1
for (i = 1; i <= d - rem; i++)
{
num = num + String.valueOf(val);
}
// In the last rem digits one is added to
// the value obtained by equal distribution
if (rem > 0)
{
val++;
for (i = d - rem + 1; i <= d; i++)
{
num = num + String.valueOf(val);
}
}
return num;
}
// Driver function
public static void main(String[] args)
{
int s = 25, d = 4;
System.out.print(findNumber(s, d));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Function to find the number having
# sum of digits as s and d number of
# digits such that the difference between
# the maximum and the minimum digit
# the minimum possible
def findNumber(s, d) :
# To store the final number
num = "";
# To store the value that is evenly
# distributed among all the digits
val = s // d;
# To store the remaining sum that still
# remains to be distributed among d digits
rem = s % d;
# rem stores the value that still remains
# to be distributed
# To keep the difference of digits minimum
# last rem digits are incremented by 1
for i in range(1, d - rem + 1) :
num = num + str(val);
# In the last rem digits one is added to
# the value obtained by equal distribution
if (rem) :
val += 1;
for i in range(d - rem + 1, d + 1) :
num = num + str(val);
return num;
# Driver function
if __name__ == "__main__" :
s = 25; d = 4;
print(findNumber(s, d));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to find the number having
// sum of digits as s and d number of
// digits such that the difference between
// the maximum and the minimum digit
// the minimum possible
static String findNumber(int s, int d)
{
// To store the readonly number
String num = "";
// To store the value that is evenly
// distributed among all the digits
int val = s / d;
// To store the remaining sum that still
// remains to be distributed among d digits
int rem = s % d;
int i;
// rem stores the value that still remains
// to be distributed
// To keep the difference of digits minimum
// last rem digits are incremented by 1
for (i = 1; i <= d - rem; i++)
{
num = num + String.Join("", val);
}
// In the last rem digits one is added to
// the value obtained by equal distribution
if (rem > 0)
{
val++;
for (i = d - rem + 1; i <= d; i++)
{
num = num + String.Join("", val);
}
}
return num;
}
// Driver function
public static void Main(String[] args)
{
int s = 25, d = 4;
Console.Write(findNumber(s, d));
}
}
// This code is contributed by 29AjayKumar
输出:
6667
时间复杂度: O(d)
辅助空间: O(1)