📌  相关文章
📜  从总共M个男性和W个女性中选择X个男性和Y个女性的总方法

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

给定四个整数XYMW。任务是找到从M个男性和W个女性中选择X个男性和Y个女性的方法。
例子:

方法:从总共M个男人中选择X个男人的总数为M C X ,从W个女人中选择Y个女人的总数为W C Y。因此,组合方式的总数将为M C X * W C Y。
下面是上述方法的实现:

C++
// C++ implementataion of the approach
#include 
using namespace std;
 
// Function to return the
// value of ncr effectively
int ncr(int n, int r)
{
 
    // Initialize the answer
    int ans = 1;
 
    for (int i = 1; i <= r; i += 1) {
 
        // Divide simultaneously by
        // i to avoid overflow
        ans *= (n - r + i);
        ans /= i;
    }
    return ans;
}
 
// Function to return the count of required ways
int totalWays(int X, int Y, int M, int W)
{
    return (ncr(M, X) * ncr(W, Y));
}
 
int main()
{
    int X = 4, Y = 3, M = 6, W = 5;
 
    cout << totalWays(X, Y, M, W);
 
    return 0;
}


Java
// JAVA implementataion of the approach
import java.io.*;
 
class GFG
{
         
    // Function to return the
    // value of ncr effectively
    static int ncr(int n, int r)
    {
     
        // Initialize the answer
        int ans = 1;
     
        for (int i = 1; i <= r; i += 1)
        {
     
            // Divide simultaneously by
            // i to avoid overflow
            ans *= (n - r + i);
            ans /= i;
        }
        return ans;
    }
     
    // Function to return the count of required ways
    static int totalWays(int X, int Y, int M, int W)
    {
        return (ncr(M, X) * ncr(W, Y));
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int X = 4, Y = 3, M = 6, W = 5;
     
        System.out.println(totalWays(X, Y, M, W));
    }
}
 
// This code is contributed by ajit_23


Python3
# Python3 implementataion of the approach
 
# Function to return the
# value of ncr effectively
def ncr(n, r):
    # Initialize the answer
    ans = 1
 
    for i in range(1,r+1):
 
        # Divide simultaneously by
        # i to avoid overflow
        ans *= (n - r + i)
        ans //= i
    return ans
 
# Function to return the count of required ways
def totalWays(X, Y, M, W):
 
    return (ncr(M, X) * ncr(W, Y))
 
X = 4
Y = 3
M = 6
W = 5
 
print(totalWays(X, Y, M, W))
 
# This code is contributed by mohit kumar 29


C#
// C# implementataion of the approach
using System;
 
class GFG
{
     
    // Function to return the
    // value of ncr effectively
    static int ncr(int n, int r)
    {
     
        // Initialize the answer
        int ans = 1;
     
        for (int i = 1; i <= r; i += 1)
        {
     
            // Divide simultaneously by
            // i to avoid overflow
            ans *= (n - r + i);
            ans /= i;
        }
        return ans;
    }
     
    // Function to return the count of required ways
    static int totalWays(int X, int Y, int M, int W)
    {
        return (ncr(M, X) * ncr(W, Y));
    }
     
    // Driver code
    static public void Main ()
    {
        int X = 4, Y = 3, M = 6, W = 5;
     
        Console.WriteLine(totalWays(X, Y, M, W));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
150