# 算法设计：给定比赛次数，查找比赛中的球队数

2021年4月13日10:01:24 发表评论 748 次浏览

## 本文概述

A将与B和C进行一场比赛
B将与C进行比赛(B已经与A进行比赛)
C已经和A队、B队打过比赛了

M = C(N, 2)
M =(N *(N – 1))/ 2
N^2 – N – 2 * M = 0

x =(-b + sqrt(b^2 – 4ac))/ 2a和x =(-b – sqrt(b^2 – 4ac))/ 2a
N = [( -1 * -1)+ sqrt((-1 * -1)–(4 * 1 *(-2 * M))))]/2
N =(1 + sqrt(1 +(8 * M))))/ 2和N =(1 – sqrt(1 +(8 * M)))/ 2

## C ++

``````//C++ implementation of the approach
#include <cmath>
#include <iostream>
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

``````<?php
//PHP implementation of the approach

//Function to return the number of teams
function number_of_teams( \$M )
{
//To store both roots of the equation

//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
\$M = 45;
echo number_of_teams( \$M );

//This code is contributed
//by chandan_jnu
?>``````

``10``