📜  包含所有不同数字的N位数字的计数

📅  最后修改于: 2021-04-22 00:24:01             🧑  作者: Mango

给定整数N ,任务是查找所有不同数字的N位数字的计数。
例子:

方法:如果N> 10 ,即将有至少一位数字将重复,因此对于这种情况,答案将为0,否则对于N = 1、2、3,…,9的值,将形成一个序列为9 ,81,648,4536,27216,136080,544320,…N项将是9 * 9! /(10 – N)!
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the factorial of n
int factorial(int n)
{
    if (n == 0)
        return 1;
    return n * factorial(n - 1);
}
 
// Function to return the count
// of n-digit numbers with
// all distinct digits
int countNum(int n)
{
    if (n > 10)
        return 0;
    return (9 * factorial(9)
            / factorial(10 - n));
}
 
// Driver code
int main()
{
    int n = 3;
 
    cout << countNum(n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
    // Function to return the factorial of n
    static int factorial(int n)
    {
        if (n == 0)
            return 1;
        return n * factorial(n - 1);
    }
     
    // Function to return the count
    // of n-digit numbers with
    // all distinct digits
    static int countNum(int n)
    {
        if (n > 10)
            return 0;
        return (9 * factorial(9) /
                    factorial(10 - n));
    }
     
    // Driver code
    public static void main(String []args)
    {
        int n = 3;
        System.out.println(countNum(n));
    }
}
 
// This code is contributed by Srathore


Python3
# Python3 implementation of the approach
 
# Function to return the factorial of n
def factorial(n) :
 
    if (n == 0) :
        return 1;
    return n * factorial(n - 1);
 
# Function to return the count
# of n-digit numbers with
# all distinct digits
def countNum(n) :
    if (n > 10) :
        return 0;
         
    return (9 * factorial(9) //
                factorial(10 - n));
 
# Driver code
if __name__ == "__main__" :
 
    n = 3;
 
    print(countNum(n));
     
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
                     
class GFG
{
     
    // Function to return the factorial of n
    static int factorial(int n)
    {
        if (n == 0)
            return 1;
        return n * factorial(n - 1);
    }
     
    // Function to return the count
    // of n-digit numbers with
    // all distinct digits
    static int countNum(int n)
    {
        if (n > 10)
            return 0;
        return (9 * factorial(9) /
                    factorial(10 - n));
    }
     
    // Driver code
    public static void Main(String []args)
    {
        int n = 3;
        Console.WriteLine(countNum(n));
    }
}
 
// This code is contributed by Princi Singh


Javascript


输出:
648

时间复杂度: O(n)