给定数字N ,任务是找到N的最大N位数倍数。
例子:
Input: N = 2
Output: 98
Explanation:
98 is the largest multiple of 2 and is of 2 digits.
Input: N = 3
Output: 999
Explanation:
999 is the largest multiple of 3 and is of 3 digits.
方法:想法是观察。
- 如果我们仔细观察,将会形成一个序列,分别是9、98、999、9996、99995,…
- 在以上系列中,第N个项可以计算为:
- 因此,将数字N作为输入并实现以上公式。
下面是上述方法的实现:
C++
// C++ program to find largest multiple
// of N containing N digits
#include
#include
using namespace std;
// Function to find the largest
// N digit multiple of N
void smallestNumber(int N)
{
cout << N * floor((pow(10, N) - 1) / N);
}
// Driver code
int main()
{
int N = 2;
smallestNumber(N);
return 0;
}
Java
// Java program to find largest multiple
// of N containing N digits
import java.util.*;
class GFG{
// Function to find the largest
// N digit multiple of N
static void smallestNumber(int N)
{
System.out.print(N * Math.floor((
Math.pow(10, N) - 1) / N));
}
// Driver code
public static void main(String args[])
{
int N = 2;
smallestNumber(N);
}
}
// This code is contributed by Nidhi_biet
Python3
# Python3 program to find largest multiple
# of N containing N digits
from math import floor
# Function to find the largest
# N digit multiple of N
def smallestNumber(N):
print(N * floor((pow(10, N) - 1) / N))
# Driver code
if __name__ == '__main__':
N = 2
smallestNumber(N)
# This code is contributed by Mohit Kumar
C#
// C# program to find largest multiple
// of N containing N digits
using System;
class GFG{
// Function to find the largest
// N digit multiple of N
static void smallestNumber(int N)
{
Console.Write(N * Math.Floor((
Math.Pow(10, N) - 1) / N));
}
// Driver code
public static void Main()
{
int N = 2;
smallestNumber(N);
}
}
// This code is contributed by Code_Mech
Javascript
输出:
98