📌  相关文章
📜  从小组中选择男性和女性组成团队的方法

📅  最后修改于: 2021-06-25 16:35:41             🧑  作者: Mango

给定四个整数n,w,m和k,其中,

  • m是总人数。
  • w是妇女总数。
  • n是组成团队所需的人员总数。
  • k是必须选择的最小人数。

任务是找到组建团队的方式。
例子:

方法:从那以后,我们必须至少要招募k个人。


从上面的第一个例子中,在7名男性和6名女性中,总共需要选择5个人,其中至少3位男性,
路数=(7C3 x 6C2)+(7C4 x 6C1)+(7C5)
= 7 x 6 x 5 x 6 x 5 +(7C3 x 6C1)+(7C2)
= 525 + 7 x 6 x 5 x 6 + 7 x 6
=(525 + 210 + 21)
= 756
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Returns factorial
// of the number
int fact(int n)
{
    int fact = 1;
    for (int i = 2; i <= n; i++)
        fact *= i;
    return fact;
}
 
// Function to calculate ncr
int ncr(int n, int r)
{
    int ncr = fact(n) / (fact(r) * fact(n - r));
    return ncr;
}
 
// Function to calculate
// the total possible ways
int ways(int m, int w, int n, int k)
{
 
    int ans = 0;
    while (m >= k) {
        ans += ncr(m, k) * ncr(w, n - k);
        k += 1;
    }
 
    return ans;
}
 
// Driver code
int main()
{
 
    int m, w, n, k;
    m = 7;
    w = 6;
    n = 5;
    k = 3;
    cout << ways(m, w, n, k);
}


Java
// Java implementation of the approach
 
import java.io.*;
 
class GFG {
 
// Returns factorial
// of the number
static int fact(int n)
{
    int fact = 1;
    for (int i = 2; i <= n; i++)
        fact *= i;
    return fact;
}
 
// Function to calculate ncr
static int ncr(int n, int r)
{
    int ncr = fact(n) / (fact(r) * fact(n - r));
    return ncr;
}
 
// Function to calculate
// the total possible ways
static int ways(int m, int w, int n, int k)
{
 
    int ans = 0;
    while (m >= k) {
        ans += ncr(m, k) * ncr(w, n - k);
        k += 1;
    }
 
    return ans;
}
 
// Driver code
    public static void main (String[] args) {
         
    int m, w, n, k;
    m = 7;
    w = 6;
    n = 5;
    k = 3;
    System.out.println( ways(m, w, n, k));
    }
}
// This Code is contributed
// by shs


Python3
# Python 3 implementation of the approach
 
# Returns factorial of the number
def fact(n):
    fact = 1
    for i in range(2, n + 1):
        fact *= i
    return fact
 
# Function to calculate ncr
def ncr(n, r):
    ncr = fact(n) // (fact(r) * fact(n - r))
    return ncr
 
# Function to calculate
# the total possible ways
def ways(m, w, n, k):
    ans = 0
    while (m >= k):
        ans += ncr(m, k) * ncr(w, n - k)
        k += 1
 
    return ans;
 
# Driver code
m = 7
w = 6
n = 5
k = 3
print(ways(m, w, n, k))
 
# This code is contributed by sahishelangia


C#
// C# implementation of the approach
 
class GFG {
 
// Returns factorial
// of the number
static int fact(int n)
{
    int fact = 1;
    for (int i = 2; i <= n; i++)
        fact *= i;
    return fact;
}
 
// Function to calculate ncr
static int ncr(int n, int r)
{
    int ncr = fact(n) / (fact(r) * fact(n - r));
    return ncr;
}
 
// Function to calculate
// the total possible ways
static int ways(int m, int w, int n, int k)
{
 
    int ans = 0;
    while (m >= k) {
        ans += ncr(m, k) * ncr(w, n - k);
        k += 1;
    }
 
    return ans;
}
 
// Driver code
    static void Main () {
         
    int m, w, n, k;
    m = 7;
    w = 6;
    n = 5;
    k = 3;
    System.Console.WriteLine( ways(m, w, n, k));
    }
}
// This Code is contributed by mits


PHP
= $k)
    {
        $ans += ncr($m, $k) *
                ncr($w, $n - $k);
        $k += 1;
    }
 
    return $ans;
}
 
// Driver code
$m = 7;
$w = 6;
$n = 5;
$k = 3;
echo ways($m, $w, $n, $k);
 
// This Code is contributed
// by Mukul Singh


Javascript


输出:
756

进一步优化:可以使用更快的算法对上述代码进行优化,以进行二项式系数计算。