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

📅  最后修改于: 2021-04-29 07:36:34             🧑  作者: Mango

给定正整数N ,任务是找到可被N整除的最小N位数字。

例子:

天真的方法:天真的方法是从最小的N位数字(例如S )迭代到最大的N位数字(例如L )。 [S,L]之间的第一个数字可被N整除是必需的结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Function to find the smallest
// N-digit number divisible by N
void smallestNumber(int N)
{
    // Find largest n digit number
    int L = pow(10, N) - 1;
 
    // Find smallest n digit number
    int S = pow(10, N - 1);
 
    for (int i = S; i <= L; i++) {
 
        // If i is divisible by N,
        // then print i and return ;
        if (i % N == 0) {
 
            cout << i;
            return;
        }
    }
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 2;
 
    // Function Call
    smallestNumber(N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the smallest
// N-digit number divisible by N
static void smallestNumber(int N)
{
 
    // Find largest n digit number
    int L = (int) (Math.pow(10, N) - 1);
 
    // Find smallest n digit number
    int S = (int) Math.pow(10, N - 1);
 
    for (int i = S; i <= L; i++)
    {
 
        // If i is divisible by N,
        // then print i and return ;
        if (i % N == 0)
        {
            System.out.print(i);
            return;
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
    // Given Number
    int N = 2;
 
    // Function Call
    smallestNumber(N);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to find the smallest
# N-digit number divisible by N
def smallestNumber(N):
 
    # Find largest n digit number
    L = pow(10, N) - 1;
 
    # Find smallest n digit number
    S = pow(10, N - 1);
 
    for i in range(S, L):
 
        # If i is divisible by N,
        # then print i and return ;
        if (i % N == 0):
            print(i);
            return;
         
# Driver Code
if __name__ == "__main__" :
     
    # Given number
    N = 2;
 
    # Function call
    smallestNumber(N)
 
# This code is contributed by rock_cool


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find the smallest
// N-digit number divisible by N
static void smallestNumber(int N)
{
 
    // Find largest n digit number
    int L = (int)(Math.Pow(10, N) - 1);
 
    // Find smallest n digit number
    int S = (int)Math.Pow(10, N - 1);
 
    for(int i = S; i <= L; i++)
    {
        
       // If i is divisible by N,
       // then print i and return ;
       if (i % N == 0)
       {
           Console.Write(i);
           return;
       }
    }
}
 
// Driver Code
public static void Main()
{
     
    // Given number
    int N = 2;
 
    // Function call
    smallestNumber(N);
}
}
 
// This code is contributed by Nidhi_biet


Javascript


C++
// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Function to find the smallest
// N-digit number divisible by N
int smallestNumber(int N)
{
 
    // Return the smallest N-digit
    // number calculated using above
    // formula
    return N * ceil(pow(10, (N - 1)) / N);
}
 
// Driver Code
int main()
{
    // Given N
    int N = 2;
 
    // Function Call
    cout << smallestNumber(N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the smallest
// N-digit number divisible by N
static int smallestNumber(int N)
{
 
    // Return the smallest N-digit
    // number calculated using above
    // formula
    return (int) (N * Math.ceil(Math.pow(10, (N - 1)) / N));
}
 
// Driver Code
public static void main(String[] args)
{
    // Given N
    int N = 2;
 
    // Function Call
    System.out.print(smallestNumber(N));
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program for the above approach
import math
 
# Function to find the smallest
# N-digit number divisible by N
def smallestNumber(N):
 
    # Return the smallest N-digit
    # number calculated using above
    # formula
    return N * math.ceil(pow(10, (N - 1)) // N);
 
# Driver Code
 
# Given N
N = 2;
 
# Function Call
print(smallestNumber(N));
 
# This code is contributed by Code_Mech


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find the smallest
// N-digit number divisible by N
static int smallestNumber(int N)
{
 
    // Return the smallest N-digit
    // number calculated using above
    // formula
    return (int) (N * Math.Ceiling(Math.Pow(10, (N - 1)) / N));
}
 
// Driver Code
public static void Main()
{
    // Given N
    int N = 2;
 
    // Function Call
    Console.Write(smallestNumber(N));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
10

时间复杂度: O(L – S) ,其中LS分别是最大和最小的N位数字。

高效的方法:如果数字可以被N整除,那么对于某个正整数X ,该数字将采用N * X的形式。
由于它必须是最小的N位数字,因此X将由下式给出:

\lceil \frac{10^{N-1}}{N} \rceil  。因此,最小的数字N位数字由下式给出:

N*\lceil \frac{10^{N-1}}{N} \rceil

例如:

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Function to find the smallest
// N-digit number divisible by N
int smallestNumber(int N)
{
 
    // Return the smallest N-digit
    // number calculated using above
    // formula
    return N * ceil(pow(10, (N - 1)) / N);
}
 
// Driver Code
int main()
{
    // Given N
    int N = 2;
 
    // Function Call
    cout << smallestNumber(N);
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the smallest
// N-digit number divisible by N
static int smallestNumber(int N)
{
 
    // Return the smallest N-digit
    // number calculated using above
    // formula
    return (int) (N * Math.ceil(Math.pow(10, (N - 1)) / N));
}
 
// Driver Code
public static void main(String[] args)
{
    // Given N
    int N = 2;
 
    // Function Call
    System.out.print(smallestNumber(N));
}
}
 
// This code is contributed by Princi Singh

Python3

# Python3 program for the above approach
import math
 
# Function to find the smallest
# N-digit number divisible by N
def smallestNumber(N):
 
    # Return the smallest N-digit
    # number calculated using above
    # formula
    return N * math.ceil(pow(10, (N - 1)) // N);
 
# Driver Code
 
# Given N
N = 2;
 
# Function Call
print(smallestNumber(N));
 
# This code is contributed by Code_Mech

C#

// C# program for the above approach
using System;
class GFG{
 
// Function to find the smallest
// N-digit number divisible by N
static int smallestNumber(int N)
{
 
    // Return the smallest N-digit
    // number calculated using above
    // formula
    return (int) (N * Math.Ceiling(Math.Pow(10, (N - 1)) / N));
}
 
// Driver Code
public static void Main()
{
    // Given N
    int N = 2;
 
    // Function Call
    Console.Write(smallestNumber(N));
}
}
 
// This code is contributed by Code_Mech

Java脚本


输出:
10

时间复杂度: O(1)