给定整数N ,任务是找到可能的N个数字的总数,使得:
- 数字的所有数字都在[0,N]范围内。
- 没有前导0。
- 一个数字中的所有数字都是唯一的。
例子:
Input: N = 2
Output: 4
10, 12, 20 and 21 are the only possible 2 digit
numbers which satisfy the given conditions.
Input: N = 5
Output: 600
方法:给定N个数字,并且可以用N种方式填充第一个位置[不能将0用作第一个数字,并且允许的数字位于[1,N]的范围内]
剩余的(N – 1)个地方可以填写N!方法
因此,可能的总数为N * N! 。
举个例子,以更好地理解。说,N = 8
第一个位置可以填充[1,8]中的任何数字,其余的7个位置可以填充8!方式即8 * 7 * 6 * 5 * 4 * 3 * 2。
因此,总计方式= 8 * 8! = 8 * 8 * 7 * 6 * 5 * 4 * 3 * 2 = 322560
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the factorial of n
int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// Function to return the
// count of numbers possible
int Count_number(int N)
{
return (N * fact(N));
}
// Driver code
int main()
{
int N = 2;
cout << Count_number(N);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
class GFG
{
// Function to return the factorial of n
static int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// Function to return the
// count of numbers possible
static int Count_number(int N)
{
return (N * fact(N));
}
// Driver code
public static void main (String[] args)
{
int N = 2;
System.out.print(Count_number(N));
}
}
// This code is contributed by anuj_67..
Python3
# Python3 implementation of the approach
# Function to return the factorial of n
def fact(n):
res = 1
for i in range(2, n + 1):
res = res * i
return res
# Function to return the
# count of numbers possible
def Count_number(N):
return (N * fact(N))
# Driver code
N = 2
print(Count_number(N))
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the factorial of n
static int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// Function to return the
// count of numbers possible
static int Count_number(int N)
{
return (N * fact(N));
}
// Driver code
public static void Main ()
{
int N = 2;
Console.WriteLine(Count_number(N));
}
}
// This code is contributed by anuj_67..
输出:
4