📌  相关文章
📜  满足给定条件的N位数字的计数

📅  最后修改于: 2021-04-30 03:23:10             🧑  作者: Mango

给定整数N ,任务是找到可能的N个数字的总数,使得:

  1. 数字的所有数字都在[0,N]范围内。
  2. 没有前导0。
  3. 一个数字中的所有数字都是唯一的。

例子:

方法:给定N个数字,并且可以用N种方式填充第一个位置[不能将0用作第一个数字,并且允许的数字位于[1,N]的范围内]
剩余的(N – 1)个地方可以填写N!方法
因此,可能的总数为N * N!

举个例子,以更好地理解。说,N = 8
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