📌  相关文章
📜  可以与给定人员组成的最大团队数

📅  最后修改于: 2021-04-26 04:59:00             🧑  作者: Mango

给定两个整数NM ,分别表示Type1Type2的人数。任务是找到可以由这两种类型的人员组成的团队的最大数量。一个团队可以包含2个Type1人和1个Type2人,或者1个Type1人和2个Type2人
例子:

方法:一种贪婪方法是从具有更多成员的组中选择2个人,并从具有较少成员的组中选择1个人,并相应地更新每个组中的人数。终止条件是无法再组队时。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true if it possible
// to form a team with the given n and m
bool canFormTeam(int n, int m)
{
 
    // 1 person of Type1 and 2 persons of Type2
    // can be chosen
    if (n >= 1 && m >= 2)
        return true;
 
    // 1 person of Type2 and 2 persons of Type1
    // can be chosen
    if (m >= 1 && n >= 2)
        return true;
 
    // Cannot from a team
    return false;
}
 
// Function to return the maximum number of teams
// that can be formed
int maxTeams(int n, int m)
{
    // To store the required count of teams formed
    int count = 0;
 
    while (canFormTeam(n, m)) {
        if (n > m) {
 
            // Choose 2 persons of Type1
            n -= 2;
 
            // And 1 person of Type2
            m -= 1;
        }
        else {
 
            // Choose 2 persons of Type2
            m -= 2;
 
            // And 1 person of Type1
            n -= 1;
        }
 
        // Another team has been formed
        count++;
    }
 
    return count;
}
 
// Driver code
int main()
{
    int n = 4, m = 5;
    cout << maxTeams(n, m);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
         
    // Function that returns true
    // if it possible to form a
    // team with the given n and m
    static boolean canFormTeam(int n, int m)
    {
     
        // 1 person of Type1 and 2 persons
        // of Type2 can be chosen
        if (n >= 1 && m >= 2)
            return true;
     
        // 1 person of Type2 and 2 persons
        // of Type1 can be chosen
        if (m >= 1 && n >= 2)
            return true;
     
        // Cannot from a team
        return false;
    }
     
    // Function to return the maximum
    // number of teams that can be formed
    static int maxTeams(int n, int m)
    {
        // To store the required count
        // of teams formed
        int count = 0;
     
        while (canFormTeam(n, m))
        {
            if (n > m)
            {
     
                // Choose 2 persons of Type1
                n -= 2;
     
                // And 1 person of Type2
                m -= 1;
            }
            else
            {
     
                // Choose 2 persons of Type2
                m -= 2;
     
                // And 1 person of Type1
                n -= 1;
            }
     
            // Another team has been formed
            count++;
        }
        return count;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 4, m = 5;
        System.out.println(maxTeams(n, m));
    }
}
 
// This code is contributed by Ryuga


Python3
# Python 3 implementation of the approach
 
# Function that returns true if it possible
# to form a team with the given n and m
def canFormTeam(n, m):
 
    # 1 person of Type1 and 2 persons of Type2
    # can be chosen
    if (n >= 1 and m >= 2):
        return True
 
    # 1 person of Type2 and 2 persons of Type1
    # can be chosen
    if (m >= 1 and n >= 2):
        return True
 
    # Cannot from a team
    return False
 
# Function to return the maximum number of teams
# that can be formed
def maxTeams(n, m):
    # To store the required count of teams formed
    count = 0
 
    while (canFormTeam(n, m)):
        if (n > m):
            # Choose 2 persons of Type1
            n -= 2
 
            # And 1 person of Type2
            m -= 1
       
        else:
            # Choose 2 persons of Type2
            m -= 2
 
            # And 1 person of Type1
            n -= 1
 
        # Another team has been formed
        count += 1
 
    return count
 
# Driver code
if __name__ == '__main__':
    n = 4
    m = 5
    print(maxTeams(n, m))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function that returns true if it possible
// to form a team with the given n and m
static bool canFormTeam(int n, int m)
{
 
    // 1 person of Type1 and 2 persons
    //  of Type2 can be chosen
    if (n >= 1 && m >= 2)
        return true;
 
    // 1 person of Type2 and 2 persons
    // of Type1 can be chosen
    if (m >= 1 && n >= 2)
        return true;
 
    // Cannot from a team
    return false;
}
 
// Function to return the maximum
// number of teams that can be formed
static int maxTeams(int n, int m)
{
    // To store the required count
    // of teams formed
    int count = 0;
 
    while (canFormTeam(n, m))
    {
        if (n > m)
        {
 
            // Choose 2 persons of Type1
            n -= 2;
 
            // And 1 person of Type2
            m -= 1;
        }
        else
        {
 
            // Choose 2 persons of Type2
            m -= 2;
 
            // And 1 person of Type1
            n -= 1;
        }
 
        // Another team has been formed
        count++;
    }
    return count;
}
 
// Driver code
public static void Main()
{
    int n = 4, m = 5;
    Console.WriteLine(maxTeams(n, m));
}
}
 
// This code is contributed by
// tufan_gupta2000


PHP
= 1 && $m >= 2)
        return true;
 
    // 1 person of Type2 and 2 persons
    // of Type1 can be chosen
    if ($m >= 1 && $n >= 2)
        return true;
 
    // Cannot from a team
    return false;
}
 
// Function to return the maximum number
// of teams that can be formed
function maxTeams($n, $m)
{
     
    // To store the required count
    // of teams formed
    $count = 0;
 
    while (canFormTeam($n, $m))
    {
        if ($n > $m)
        {
 
            // Choose 2 persons of Type1
            $n -= 2;
 
            // And 1 person of Type2
            $m -= 1;
        }
        else
        {
 
            // Choose 2 persons of Type2
            $m -= 2;
 
            // And 1 person of Type1
            $n -= 1;
        }
 
        // Another team has been formed
        $count++;
    }
 
    return $count;
}
 
// Driver code
$n = 4;
$m = 5;
echo maxTeams($n, $m);
 
// This code is contributed by mits
?>


Javascript


输出:
3