📌  相关文章
📜  查找联赛中排名第 K 的球队可能获得的最高分

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

查找联赛中排名第 K 的球队可能获得的最高分

联赛中有N支队伍,从1N编号。给定一个整数K ,任务是找出一个在第 K位完成的球队可以得分的最高分。
假设球队获胜得2分,失败得0分。

注意:联赛也称为循环赛,每支球队与其他球队只交手一次。

例子:

方法:问题的解决基于以下观察:

按照下面提到的步骤来实现上述观察:

  • 对于给定的NK值,在第 K完成的球队的最大可能获胜次数为(2*N – K -1)/2
  • 现在通过将计算出的最大可能获胜乘以2来计算最终得分。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate
// maximum points scored by the
// team finishing at kth position
int maxScore(int N, int K)
{
    // Calculate Maximum wins
    // using formula
    int maxWins = (2 * N - K - 1) / 2;
 
    // Multiply max wins by 2
    // to get total points
    int totalPoints = maxWins * 2;
 
    return totalPoints;
}
 
// Driver code
int main()
{
    int N = 5;
    int k = 3;
 
    cout << maxScore(N, k);
    return 0;
}


Java
// JAVA program for the above approach
import java.util.*;
class GFG
{
 
  // Function to calculate
  // maximum points scored by the
  // team finishing at kth position
  public static int maxScore(int N, int K)
  {
 
    // Calculate Maximum wins
    // using formula
    int maxWins = (2 * N - K - 1) / 2;
 
    // Multiply max wins by 2
    // to get total points
    int totalPoints = maxWins * 2;
 
    return totalPoints;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int N = 5;
    int k = 3;
    System.out.print(maxScore(N, k));
  }
}
 
// This code is contributed by Taranpreet


Python3
# Python code for the above approach
 
# Function to calculate
# maximum points scored by the
# team finishing at kth position
def maxScore(N, K):
 
    # Calculate Maximum wins
    # using formula
    maxWins = (2 * N - K - 1) / 2;
 
    # Multiply max wins by 2
    # to get total points
    totalPoints = maxWins * 2;
 
    return int(totalPoints)
 
# Driver code
N = 5;
k = 3;
 
print(maxScore(N, k));
 
# This code is contributed by Saurabh jaiswal


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to calculate
  // maximum points scored by the
  // team finishing at kth position
  static int maxScore(int N, int K)
  {
 
    // Calculate Maximum wins
    // using formula
    int maxWins = (2 * N - K - 1) / 2;
 
    // Multiply max wins by 2
    // to get total points
    int totalPoints = maxWins * 2;
 
    return totalPoints;
  }
 
  // Driver code
  public static void Main()
  {
    int N = 5;
    int k = 3;
 
    Console.Write(maxScore(N, k));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
6

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