📜  获胜者玩的最大游戏数

📅  最后修改于: 2021-04-23 22:29:11             🧑  作者: Mango

有N位玩家正在参加比赛。我们需要找到获胜者可以玩的最大游戏数。在此锦标赛中,只有两名玩家玩的游戏之间的差异不超过一个时,才允许他们对战。

例子:

Input  : N = 3
Output : 2
Maximum games winner can play = 2
Assume that player are P1, P2 and P3
First, two players will play let (P1, P2)
Now winner will play against P3, 
making total games played by winner = 2

Input  : N = 4
Output : 2
Maximum games winner can play = 2
Assume that player are P1, P2, P3 and P4
First two pairs will play lets (P1, P2) and 
(P3, P4). Now winner of these two games will 
play against each other, making total games
played by winner = 2

我们可以通过首先计算所需的最小玩家数来解决此问题,以使获胜者进行x场比赛。一旦计算出了实际问题,则恰好与此相反。现在假设dp [i]表示所需的最少玩家人数,以便获胜者进行i场比赛。我们可以在dp值之间编写一个递归关系,如下所示:
dp [i + 1] = dp [i] + dp [i – 1],因为如果亚军玩了(i – 1)场比赛,而获胜者参加了i场比赛,那么与他们进行比赛的所有球员都是不相交的,总计获胜者玩的游戏将是这两组玩家的加法。
上面的递归关系可以写成dp [i] = dp [i – 1] + dp [i – 2]
这与斐波那契数列关系相同,因此我们的最终答案将是最大斐波那契数的索引,该数字小于或等于输入中给定的玩家数。

C++
// C/C++ program to find maximum number of 
// games played by winner
#include 
using namespace std;
  
// method returns maximum games a winner needs
// to play in N-player tournament
int maxGameByWinner(int N)
{
    int dp[N];
  
    // for 0 games, 1 player is needed
    // for 1 game, 2 players are required
    dp[0] = 1;    
    dp[1] = 2;
      
    // loop until i-th Fibonacci number is  
    // less than or equal to N
    int i = 2;
    do {
        dp[i] = dp[i - 1] + dp[i - 2];
    } while (dp[i++] <= N);
  
    // result is (i - 2) because i will be 
    // incremented one extra in while loop
    // and we want the last value which is 
    // smaller than N, so one more decrement
    return (i - 2);
}
  
// Driver code to test above methods
int main()
{
    int N = 10;
    cout << maxGameByWinner(N) << endl;
    return 0;
}


Java
// Java program to find maximum number of 
// games played by winner
class Max_game_winner {
  
    // method returns maximum games a winner needs
    // to play in N-player tournament
    static int maxGameByWinner(int N)
    {
        int[] dp = new int[N];
       
        // for 0 games, 1 player is needed
        // for 1 game, 2 players are required
        dp[0] = 1;    
        dp[1] = 2;
           
        // loop until i-th Fibonacci number is  
        // less than or equal to N
        int i = 2;
        do {
            dp[i] = dp[i - 1] + dp[i - 2];
        } while (dp[i++] <= N);
       
        // result is (i - 2) because i will be 
        // incremented one extra in while loop
        // and we want the last value which is 
        // smaller than N, so one more decrement
        return (i - 2);
    }
       
    // Driver code to test above methods
    public static void main(String args[])
    {
        int N = 10;
        System.out.println(maxGameByWinner(N));
    }
}
//This code is contributed by Sumit Ghosh


Python3
# Python3 program to find maximum 
# number of games played by winner 
  
# method returns maximum games 
# a winner needs to play in 
# N-player tournament 
  
def maxGameByWinner(N):
    dp = [0 for i in range(N)]
      
    # for 0 games, 1 player is needed
    # for 1 game, 2 players are required 
    dp[0] = 1
    dp[1] = 2
      
    # loop until i-th Fibonacci 
    # number is less than or 
    # equal to N 
    i = 1
    while dp[i] <= N:
        i = i + 1
        dp[i] = dp[i - 1] + dp[i - 2]
          
    # result is (i - 1) because i will be
    # incremented one extra in while loop 
    # and we want the last value which is 
    # smaller than N, so 
    return (i - 1)
  
# Driver code
N = 10
print(maxGameByWinner(N))
  
# This code is contributed 
# by sahilshelangia


C#
// C# program to find maximum number
// of games played by winner
using System;
  
class GFG {
      
    // method returns maximum games a 
    // winner needs to play in N-player
    // tournament
    static int maxGameByWinner(int N)
    {
        int[] dp = new int[N];
      
        // for 0 games, 1 player is needed
        // for 1 game, 2 players are required
        dp[0] = 1; 
        dp[1] = 2;
          
        // loop until i-th Fibonacci number 
        // is less than or equal to N
        int i = 2;
          
        do {
            dp[i] = dp[i - 1] + dp[i - 2];
        } while (dp[i++] <= N);
      
        // result is (i - 2) because i will be 
        // incremented one extra in while loop
        // and we want the last value which is 
        // smaller than N, so one more decrement
        return (i - 2);
    }
      
    // Driver code
    public static void Main()
    {
        int N = 10;
        Console.Write(maxGameByWinner(N));
    }
}
  
// This code is contributed by Nitin Mittal.


PHP


输出 :

4