找到获胜者所需的匹配次数
给定一个数字N ,代表参加羽毛球比赛的球员人数。任务是确定确定获胜者所需的匹配次数。在每场比赛 1 中,球员被淘汰。
例子:
Input: N = 4
Output: Matches played = 3
(As after each match only N - 1 players left)
Input: N = 9
Output: Matches played = 8
方法:因为,在每场比赛后,一名球员被淘汰出局。因此,要获得胜利者,应淘汰n-1名球员并进行n-1场比赛。
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
// Function that will tell
// no. of matches required
int noOfMatches(int N)
{
return N - 1;
}
// Driver code
int main()
{
int N = 8;
cout << "Matches played = " << noOfMatches(N);
return 0;
}
Java
// Java implementation of above approach
import java.io.*;
class GFG
{
// Function that will tell
// no. of matches required
static int noOfMatches(int N)
{
return N - 1;
}
// Driver code
public static void main (String[] args)
{
int N = 8;
System.out.println ("Matches played = " +
noOfMatches(N));
}
}
// This code is contributed by jit_t
Python3
# Python 3 implementation of
# above approach
# Function that will tell
# no. of matches required
def noOfMatches(N) :
return N - 1
# Driver code
if __name__ == "__main__" :
N = 8
print("Matches played =",
noOfMatches(N))
# This code is contributed
# by ANKITRAI1
C#
//C# implementation of above approach
using System;
public class GFG{
// Function that will tell
// no. of matches required
static int noOfMatches(int N)
{
return N - 1;
}
// Driver code
static public void Main (){
int N = 8;
Console.WriteLine("Matches played = " +
noOfMatches(N));
}
}
// This code is contributed by ajit
PHP
Javascript
输出:
Matches played = 7