📜  最小偶数N位

📅  最后修改于: 2021-04-24 04:21:39             🧑  作者: Mango

给定数字N ,任务是找到N个数字的最小偶数。
例子:

Input: N = 1
Output: 0

Input: N = 2
Output: 10 

方法
情况1 :如果N = 1,则答案将为0。
情况2 :如果N!= 1,则答案将是(10 ^(N-1)),因为最小的偶数序列将继续,如0、10、100、1000、10000、100000等。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return smallest even
// number with n digits
int smallestEven(int n)
{
    if (n == 1)
        return 0;
    return pow(10, n - 1);
}
 
// Driver Code
int main()
{
 
    int n = 4;
    cout << smallestEven(n);
 
    return 0;
}


Java
// Java implementation of the approach
class Solution {
 
    // Function to return smallest even
    // number with n digits
    static int smallestEven(int n)
    {
        if (n == 1)
            return 0;
        return Math.pow(10, n - 1);
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 4;
        System.out.println(smallestEven(n));
    }
}


Python3
# Python3 implementation of
# the approach
 
# Function to return smallest
# even number with n digits
def smallestEven(n) :
 
    if (n == 1):
        return 0
    return pow(10, n - 1)
 
# Driver Code
n = 4
print(smallestEven(n))
 
# This code is contributed
# by ihritik.


C#
// C# implementation of the approach
using System;
class Solution {
 
    // Function to return smallest even
    // number with n digits
    static int smallestEven(int n)
    {
        if (n == 1)
            return 0;
        return Math.pow(10, n - 1);
    }
 
    // Driver code
    public static void Main()
    {
        int n = 4;
        Console.Write(smallestEven(n));
    }
}


PHP


Javascript


输出:
1000

时间复杂度: O(1)。