找出最小的数,使得它的数字之和为 N 并且它可以被整除 .
例子 :
Input : N = 5
Output : 500000
500000 is the smallest number divisible
by 10^5 and sum of digits as 5.
Input : N = 20
Output : 29900000000000000000000
解释
使一个数被整除我们需要在数字末尾至少有 N 个零。为了使数字最小,我们在数字末尾添加了 N 个零。现在,我们需要确保数字之和为 N。为此,我们将尝试使数字的长度尽可能小以获得答案。因此,我们不断向数字中插入 9,直到总和不超过 N。如果我们还有余数,那么我们将其保留为第一个数字(最重要的数字),以便使结果数字最小化。
该方法适用于所有子任务,但有两种极端情况:
1. 首先是最终数字可能不适合 C++/ Java存在的数据类型。由于我们只需要输出数字,我们可以使用字符串来存储答案。
2. 答案为 0 的唯一极端情况是 N = 0。
3. 没有答案不存在的情况。
C++
// CPP program to find smallest
// number to find smallest number
// with N as sum of digits and
// divisible by 10^N.
#include
using namespace std;
void digitsNum(int N)
{
// If N = 0 the string will be 0
if (N == 0)
cout << "0\n";
// If n is not perfectly divisible
// by 9 output the remainder
if (N % 9 != 0)
cout << (N % 9);
// Print 9 N/9 times
for (int i = 1; i <= (N / 9); ++i)
cout << "9";
// Append N zero's to the number so
// as to make it divisible by 10^N
for (int i = 1; i <= N; ++i)
cout << "0";
cout << "\n";
}
// Driver Code
int main()
{
int N = 5;
cout << "The number is : ";
digitsNum(N);
return 0;
}
Java
// Java program to find smallest
// number to find smallest number
// with N as sum of digits and
// divisible by 10^N.
import java.io.*;
class GFG
{
static void digitsNum(int N)
{
// If N = 0 the string will be 0
if (N == 0)
System.out.println("0");
// If n is not perfectly divisible
// by 9 output the remainder
if (N % 9 != 0)
System.out.print((N % 9));
// Print 9 N/9 times
for (int i = 1; i <= (N / 9); ++i)
System.out.print("9");
// Append N zero's to the number so
// as to make it divisible by 10^N
for (int i = 1; i <= N; ++i)
System.out.print("0");
System.out.print("" );
}
// Driver Code
public static void main (String[] args)
{
int N = 5;
System.out.print("The number is : ");
digitsNum(N);
}
}
// This code is contributed by vt_m
Python3
# Python program to find smallest
# number to find smallest number
# with N as sum of digits and
# divisible by 10^N.
import math
def digitsNum(N):
# If N = 0 the string will be 0
if (N == 0) :
print("0", end = "")
# If n is not perfectly divisible
# by 9 output the remainder
if (N % 9 != 0):
print (N % 9, end ="")
# Print 9 N/9 times
for i in range( 1, int(N / 9) + 1) :
print("9", end = "")
# Append N zero's to the number so
# as to make it divisible by 10^N
for i in range(1, N + 1) :
print("0", end = "")
print()
# Driver Code
N = 5
print("The number is : ",end="")
digitsNum(N)
# This code is contributed by Gitanjali.
C#
// C# program to find smallest
// number to find smallest number
// with N as sum of digits and
// divisible by 10^N.
using System;
class GFG
{
static void digitsNum(int N)
{
// If N = 0 the string will be 0
if (N == 0)
Console.Write("0");
// If n is not perfectly divisible
// by 9 output the remainder
if (N % 9 != 0)
Console.Write((N % 9));
// Print 9 N/9 times
for (int i = 1; i <= (N / 9); ++i)
Console.Write("9");
// Append N zero's to the number so
// as to make it divisible by 10^N )
for (int i = 1; i <= N; ++i)
Console.Write("0");
Console.WriteLine("" );
}
// Driver Code
public static void Main ()
{
int N = 5;
Console.Write("The number is : ");
digitsNum(N);
}
}
// This code is contributed by vt_m
PHP
Javascript
输出 :
The number is : 500000
时间复杂度: O(N)