给定整数N≥1 ,任务是找到最小的N位数字,该数字是5的倍数。
例子:
Input: N = 1
Output: 5
Input: N = 2
Output: 10
Input: N = 3
Output: 100
方法:
- 如果N = 1,则答案为5 。
- 如果N> 1,那么答案将是(10 (N – 1) ),因为最小的5的倍数的序列将继续为10、100、1000、10000、100000等。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the smallest n digit
// number which is a multiple of 5
int smallestMultiple(int n)
{
if (n == 1)
return 5;
return pow(10, n - 1);
}
// Driver code
int main()
{
int n = 4;
cout << smallestMultiple(n);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to return the smallest n digit
// number which is a multiple of 5
static int smallestMultiple(int n)
{
if (n == 1)
return 5;
return (int)(Math.pow(10, n - 1));
}
// Driver code
public static void main(String args[])
{
int n = 4;
System.out.println(smallestMultiple(n));
}
}
Python3
# Python3 implementation of the approach
# Function to return the smallest n digit
# number which is a multiple of 5
def smallestMultiple(n):
if (n == 1):
return 5
return pow(10, n - 1)
# Driver code
n = 4
print(smallestMultiple(n))
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the smallest n digit
// number which is a multiple of 5
static int smallestMultiple(int n)
{
if (n == 1)
return 5;
return (int)(Math.Pow(10, n - 1));
}
// Driver code
public static void Main()
{
int n = 4;
Console.Write(smallestMultiple(n));
}
}
PHP
Javascript
输出:
1000
时间复杂度: O(1)