📜  给定比赛次数,查找比赛中的球队数

📅  最后修改于: 2021-05-04 14:45:20             🧑  作者: Mango

给定一个整数M ,它是锦标赛中进行比赛的次数,并且每个参与团队都与其他所有团队进行过比赛。任务是查找比赛中有多少支球队。
例子:

方法:由于每次比赛都是在两支球队之间进行的。因此,此问题类似于从给定的N个对象中选择2个对象。因此,比赛的总次数将为C(N,2),其中N是参赛球队的数量。所以,

解完上述两个方程式后,我们将获得两个N值。一个值将为正,而一个值为负。忽略负值。因此,团队数量将是上述方程式的正根。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#include 
using namespace std;
 
// Function to return the number of teams
int number_of_teams(int M)
{
    // To store both roots of the equation
    int N1, N2, sqr;
 
    // sqrt(b^2 - 4ac)
    sqr = sqrt(1 + (8 * M));
 
    // First root (-b + sqrt(b^2 - 4ac)) / 2a
    N1 = (1 + sqr) / 2;
 
    // Second root (-b - sqrt(b^2 - 4ac)) / 2a
    N2 = (1 - sqr) / 2;
 
    // Return the positive root
    if (N1 > 0)
        return N1;
    return N2;
}
 
// Driver code
int main()
{
    int M = 45;
    cout << number_of_teams(M);
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
 
// Function to return the number of teams
static int number_of_teams(int M)
{
    // To store both roots of the equation
    int N1, N2, sqr;
 
    // sqrt(b^2 - 4ac)
    sqr = (int)Math.sqrt(1 + (8 * M));
 
    // First root (-b + sqrt(b^2 - 4ac)) / 2a
    N1 = (1 + sqr) / 2;
 
    // Second root (-b - sqrt(b^2 - 4ac)) / 2a
    N2 = (1 - sqr) / 2;
 
    // Return the positive root
    if (N1 > 0)
        return N1;
    return N2;
}
 
    // Driver code
    public static void main (String[] args)
    {
        int M = 45;
        System.out.println( number_of_teams(M));
    }
}
 
// this code is contributed by vt_m..


Python3
# Python implementation of the approach
import math
 
# Function to return the number of teams
def number_of_teams(M):
     
    # To store both roots of the equation
    N1, N2, sqr = 0,0,0
 
    # sqrt(b^2 - 4ac)
    sqr = math.sqrt(1 + (8 * M))
 
    # First root (-b + sqrt(b^2 - 4ac)) / 2a
    N1 = (1 + sqr) / 2
 
    # Second root (-b - sqrt(b^2 - 4ac)) / 2a
    N2 = (1 - sqr) / 2
 
    # Return the positive root
    if (N1 > 0):
        return int(N1)
    return int(N2)
 
# Driver code
def main():
    M = 45
    print(number_of_teams(M))
if __name__ == '__main__':
    main()
     
# This code has been contributed by 29AjayKumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the number of teams
    static int number_of_teams(int M)
    {
        // To store both roots of the equation
        int N1, N2, sqr;
     
        // sqrt(b^2 - 4ac)
        sqr = (int)Math.Sqrt(1 + (8 * M));
     
        // First root (-b + sqrt(b^2 - 4ac)) / 2a
        N1 = (1 + sqr) / 2;
     
        // Second root (-b - sqrt(b^2 - 4ac)) / 2a
        N2 = (1 - sqr) / 2;
     
        // Return the positive root
        if (N1 > 0)
            return N1;
        return N2;
    }
     
    // Driver code
    public static void Main()
    {
        int M = 45;
        Console.WriteLine( number_of_teams(M));
    }
}
 
// This code is contributed by Ryuga


PHP
 0)
        return $N1;
    return $N2;
}
 
// Driver code
$M = 45;
echo number_of_teams($M);
 
// This code is contributed
// by chandan_jnu
?>


Javascript


输出:
10