给定四个整数X , Y , M和N。任务是通过从M个元音和N个辅音的总数中选择X个元音和Y个辅音来找到形成单词的方式。
例子:
Input : X = 2, Y = 2, M = 3, N = 3
Output : 216
The total number of ways of choosing 2 vowels from a total number of 3 vowels is i.e 3
The total number of ways of choosing 2 consonants from a total number of 3 consonants is i.e 3.
The total number of ways of selecting 2 consonants from 3 and 2 vowels from 3 is * = 9
The total number of ways of arranging 4 letters among themselves = 4! = 24
Hence, the required number of ways = 24 * 9 = 216
Input : X = 1, Y = 2, M = 2, N = 3
Output : 36
建议:在继续解决方案之前,请先在{IDE}上尝试使用您的方法。
方法 :
- 从M个元音的总数中选择X个元音的方式总数为
- 从N个辅音的总数中选择Y个辅音的方式总数为
- 从N个元音中选择Y个辅音和从M个元音中选择X个辅音的方法总数为 *
- 彼此之间排列(X + Y)个字母的总数=(X + Y)!
- 因此,所需的路数=(X + Y)! * *
下面是上述方法的实现:
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