📌  相关文章
📜  查找可以由M个元音和N个辅音形成的X个元音和Y个辅音的单词数

📅  最后修改于: 2021-05-06 03:19:07             🧑  作者: Mango

给定四个整数XYMN。任务是通过从M个元音和N个辅音的总数中选择X个元音和Y个辅音来找到形成单词的方式。

例子:

建议:在继续解决方案之前,请先在{IDE}上尝试使用您的方法。

方法 :

  • 从M个元音的总数中选择X个元音的方式总数为M\choose X
  • 从N个辅音的总数中选择Y个辅音的方式总数为N\choose Y
  • 从N个元音中选择Y个辅音和从M个元音中选择X个辅音的方法总数为N\choose Y * M\choose X
  • 彼此之间排列(X + Y)个字母的总数=(X + Y)!
  • 因此,所需的路数=(X + Y)! * N\choose Y * M\choose X

下面是上述方法的实现:

C++
// CPP program to find the number of words
// of X vowels  and Y consonants can be
// formed from M vowels and N consonants
#include 
using namespace std;
  
// Function to returns factorial of n
int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
  
// Function to find nCr
int nCr(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
  
// Function to find the number of words
// of X vowels  and Y consonants can be
// formed from M vowels and N consonants
int NumberOfWays(int X, int Y, int M, int N)
{
    return fact(X + Y) * nCr(M, X) * nCr(N, Y);
}
  
// Driver code
int main()
{
    int X = 2, Y = 2, M = 3, N = 3;
  
    // Function call
    cout << NumberOfWays(X, Y, M, N);
  
    return 0;
}


Java
// Java program to find the number of words 
// of X vowels and Y consonants can be 
// formed from M vowels and N consonants
import java.util.*;
import java.lang.*;
import java.io.*;
  
class GFG
{
      
    // Function to returns 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 find nCr 
    static int nCr(int n, int r) 
    { 
        return fact(n) / (fact(r) * 
                          fact(n - r)); 
    } 
      
    // Function to find the number of words 
    // of X vowels and Y consonants can be 
    // formed from M vowels and N consonants 
    static int NumberOfWays(int X, int Y, 
                            int M, int N) 
    { 
        return fact(X + Y) * nCr(M, X) * 
                             nCr(N, Y); 
    } 
      
      
    // Driver code
    public static void main (String[] args) 
                  throws java.lang.Exception
    {
        int X = 2, Y = 2, M = 3, N = 3; 
      
        // Function call 
        System.out.println(NumberOfWays(X, Y, M, N));         
    }
}
  
// This code is contributed by Nidhiva


Python3
# Python 3 program to find the number of words
# of X vowels and Y consonants can be
# formed from M vowels and N consonants
  
# Function to returns factorial of n
def fact(n):
    res = 1
    for i in range(2, n + 1, 1):
        res = res * i
    return res
  
# Function to find nCr
def nCr(n, r):
    return fact(n) // (fact(r) * fact(n - r))
  
# Function to find the number of words
# of X vowels and Y consonants can be
# formed from M vowels and N consonants
def NumberOfWays(X, Y, M, N):
    return fact(X + Y) * nCr(M, X) * nCr(N, Y)
  
# Driver code
if __name__ == '__main__':
    X = 2
    Y = 2
    M = 3
    N = 3
  
    # Function call
    print(NumberOfWays(X, Y, M, N))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find the number of words 
// of X vowels and Y consonants can be 
// formed from M vowels and N consonants
using System;
  
class GFG
{
      
    // Function to returns 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 find nCr 
    static int nCr(int n, int r) 
    { 
        return fact(n) / (fact(r) * 
                          fact(n - r)); 
    } 
      
    // Function to find the number of words 
    // of X vowels and Y consonants can be 
    // formed from M vowels and N consonants 
    static int NumberOfWays(int X, int Y, 
                            int M, int N) 
    { 
        return fact(X + Y) * nCr(M, X) * 
                             nCr(N, Y); 
    } 
      
    // Driver code
    public static void Main (String[] args)
    {
        int X = 2, Y = 2, M = 3, N = 3; 
      
        // Function call 
        Console.WriteLine(NumberOfWays(X, Y, M, N));         
    }
}
  
// This code is contributed by 29AjayKumar


输出:
216