📜  计算具有N 0和M 1且无前导零的数字

📅  最后修改于: 2021-06-26 12:36:01             🧑  作者: Mango

给定两个整数NM ,任务是查找具有N 0和M 1且无前导零和N + M总位数的不同数字的数量。

例子:

方法:通过找到N个相似项和M – 1个相似项的总排列,可以轻松解决该问题。由于不允许前导零,因此数字1的开头始终固定为1 。其余的M – 1 1和N 0以不同的排列方式排列。现在,排列的(A + B)对象的数量,其中A的对象相同的类型和其它类型的对象通过(A + B)中给出! /(A!* B!) 。因此,不同数字的数量由(N + M -1)给出! /(N!*(M – 1)!)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define ll long long int
 
// Function to return the factorial of a number
ll factorial(int f)
{
    ll fact = 1;
 
    for (int i = 2; i <= f; i++)
        fact *= (ll)i;
 
    return fact;
}
 
// Function to return the count of distinct
// (N + M) digit numbers having N 0's
// and and M 1's with no leading zeros
ll findPermutation(int N, int M)
{
    ll permutation = factorial(N + M - 1)
                     / (factorial(N) * factorial(M - 1));
    return permutation;
}
 
// Driver code
int main()
{
    int N = 3, M = 3;
    cout << findPermutation(N, M);
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
// Function to return the factorial of a number
static int factorial(int f)
{
    int fact = 1;
 
    for (int i = 2; i <= f; i++)
        fact *= (int)i;
 
    return fact;
}
 
// Function to return the count of distinct
// (N + M) digit numbers having N 0's
// and and M 1's with no leading zeros
static int findPermutation(int N, int M)
{
    int permutation = factorial(N + M - 1)
                    / (factorial(N) * factorial(M - 1));
    return permutation;
}
 
// Driver code
public static void main (String[] args)
{
 
    int N = 3, M = 3;
    System.out.println(findPermutation(N, M));
}
}
 
// This code is contributed
// by ajit


Python3
# Python3 implementation of the approach
 
# Function to return the factorial
# of a number
def factorial(f):
    fact = 1
    for i in range(2, f + 1):
        fact *= i
    return fact
 
# Function to return the count of distinct
# (N + M) digit numbers having N 0's
# and and M 1's with no leading zeros
def findPermuatation(N, M):
    permutation = (factorial(N + M - 1) //
                  (factorial(N) * factorial(M - 1)))
    return permutation
 
# Driver code
N = 3; M = 3
print(findPermuatation(N, M))
 
# This code is contributed
# by Shrikant13


C#
// C# implementation of the approach
using System;
 
class GFG
{
// Function to return the factorial of a number
static int factorial(int f)
{
    int fact = 1;
 
    for (int i = 2; i <= f; i++)
        fact *= (int)i;
 
    return fact;
}
 
// Function to return the count of distinct
// (N + M) digit numbers having N 0's
// and and M 1's with no leading zeros
static int findPermutation(int N, int M)
{
    int permutation = factorial(N + M - 1)
                    / (factorial(N) * factorial(M - 1));
    return permutation;
}
 
// Driver code
public static void Main()
{
    int N = 3, M = 3;
    Console.Write(findPermutation(N, M));
}
}
 
// This code is contributed
// by Akanksha Rai


PHP


Javascript


输出:
10

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。