📜  组织比赛问题

📅  最后修改于: 2022-05-13 01:56:05.481000             🧑  作者: Mango

组织比赛问题

给定一个正整数N ,表示玩游戏的玩家数量。比赛在两支球队之间进行,每支球队至少由一名球员组成,但比赛中球员的总数必须是N 。游戏持续时间正好是30 分钟,任务是检查所有玩家是否会互相玩游戏,如果游戏可以玩到T小时,并且允许玩游戏超过1次。如果发现是真的,则打印“可能” 。否则,打印“不可能”

例子:

方法:可以使用贪心技术解决问题。以下是观察结果:

请按照以下步骤解决问题:

  • 如果玩游戏N-1次的总时间小于或等于T ,则打印“Possible”
  • 如果玩游戏的总时间(N + 1) / 2次小于或等于T ,则打印“可能”
  • 否则,打印“不可能”

下面是上述方法的实现:

C++
// C++ Program for the above approach
#include 
using namespace std;
 
// Function to find the N players
// the game against each other or not
string calculate(int N, int T)
{
   
   // Base Case
    if (N <= 1 || T <= 0) {
       
      // Return -1 if not valid
        return "-1";
    }
   
  // Converting hours into minutes
    int minutes = T * 60;
   
   // Calculating maximum games that
    // can be played.
    int max_match = N - 1;
   
  // Time required for conducting
    // maximum games
    int max_time = max_match * 30;
 
  // Checking if it is possible
    // to conduct maximum games
    if (max_time <= minutes) {
       
      // Return possible
        return "Possible";
    }
 
  // Calculating minimum games
    int min_match = N / 2;
    min_match = N - min_match;
   
  // Time required for conducting
    // minimum games
    int min_time = min_match * 30;
 
  // Checking if it is possible
   // to conduct minimum games
    if (min_time <= minutes) {
       
      // Return possible
        return "Possible";
    }
 
  // Return not possible if time
   // is less than required time
    return "Not Possible";
}
 
 // Driver Code
 // Total count of players
int main()
{
    int N = 6, T = 2;
   
  // function call
    cout << calculate(N, T);
    return 0;
}
 
// This code is contributed by Parth Manchanda


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
// Function to find the N players
// the game against each other or not
static String calculate(int N, int T)
{
   
   // Base Case
    if (N <= 1 || T <= 0) {
       
      // Return -1 if not valid
        return "-1";
    }
   
  // Converting hours into minutes
    int minutes = T * 60;
   
   // Calculating maximum games that
    // can be played.
    int max_match = N - 1;
   
  // Time required for conducting
    // maximum games
    int max_time = max_match * 30;
 
  // Checking if it is possible
    // to conduct maximum games
    if (max_time <= minutes) {
       
      // Return possible
        return "Possible";
    }
 
  // Calculating minimum games
    int min_match = N / 2;
    min_match = N - min_match;
   
  // Time required for conducting
    // minimum games
    int min_time = min_match * 30;
 
  // Checking if it is possible
   // to conduct minimum games
    if (min_time <= minutes) {
       
      // Return possible
        return "Possible";
    }
 
  // Return not possible if time
   // is less than required time
    return "Not Possible";
}
 
// Driver code
public static void main(String[] args)
{
    int N = 6, T = 2;
   
    // function call
    System.out.println(calculate(N, T));
}
}
 
// This code is contributed by sanjoy_62.


Python3
# Python program for the above problem
 
 
 
# Function to find the N players
# the game against each other or not
def calculate(N, T):
 
 
    # Base Case
    if N <= 1 or T <= 0:
 
        # Return -1 if not valid
        return -1
 
 
    # Converting hours into minutes
    minutes = T * 60
 
 
    # Calculating maximum games that
    # can be played.
    max_match = N - 1
 
 
    # Time required for conducting
    # maximum games
    max_time = max_match * 30
 
 
    # Checking if it is possible
    # to conduct maximum games
    if max_time <= minutes:
 
 
        # Return possible
        return "Possible"
 
 
    # Calculating minimum games
    min_match = N//2
    min_match = N - min_match
 
 
    # Time required for conducting
    # minimum games
    min_time = min_match * 30
 
 
    # Checking if it is possible
    # to conduct minimum games
    if min_time <= minutes:
 
 
        # Return possible
        return "Possible"
 
 
    # Return not possible if time
    # is less than required time
    return "Not Possible"
 
 
 
# Driver Code
if __name__ == "__main__":
 
 
    # Total count of players
    N = 6
 
 
    # Given hours
    T = 2
 
 
    # Function call
    ans = calculate(N, T)
 
 
    # Print ans
    print(ans)


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the N players
// the game against each other or not
static string calculate(int N, int T)
{
   
   // Base Case
    if (N <= 1 || T <= 0) {
       
      // Return -1 if not valid
        return "-1";
    }
   
  // Converting hours into minutes
    int minutes = T * 60;
   
   // Calculating maximum games that
    // can be played.
    int max_match = N - 1;
   
  // Time required for conducting
    // maximum games
    int max_time = max_match * 30;
 
  // Checking if it is possible
    // to conduct maximum games
    if (max_time <= minutes) {
       
      // Return possible
        return "Possible";
    }
 
  // Calculating minimum games
    int min_match = N / 2;
    min_match = N - min_match;
   
  // Time required for conducting
    // minimum games
    int min_time = min_match * 30;
 
  // Checking if it is possible
   // to conduct minimum games
    if (min_time <= minutes) {
       
      // Return possible
        return "Possible";
    }
 
  // Return not possible if time
   // is less than required time
    return "Not Possible";
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 6, T = 2;
   
  // function call
    Console.WriteLine(calculate(N, T));
}
}
 
// This code is contributed by splevel62.


Javascript


输出:
Possible

时间复杂度: O(1)
辅助空间: O(1)