📌  相关文章
📜  位数为N且可被10 ^ N整除的最小数字

📅  最后修改于: 2021-06-26 18:49:34             🧑  作者: Mango

找出最小的数字,使得其数字的总和为N,并且可以被整数整除10^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

解释
使数字可被10^N  在数字的末尾至少需要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)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。