📌  相关文章
📜  通过连续排列球来确定游戏的获胜者

📅  最后修改于: 2021-05-08 17:40:46             🧑  作者: Mango

给定分别为小球和大球NM的数量,任务是通过进行以下两个动作来确定如果XY双方都发挥出最佳状态,则哪个玩家获胜:

  • 玩家X将尝试保持相同类型的球,即小球后跟另一个小球,大球后跟一个大球。
  • 玩家Y将放不同类型的球,即先放小,再放大球,反之亦然。

无法采取行动的玩家将输掉比赛。任务是找出给定球数赢得比赛的玩家。假定玩家X总是最先开始。

例子:

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

  • 检查小球数量是否大于或等于大球数量,那么玩家X的得分为N – 1,因为玩家X首先放置小球,然后玩家Y立即放置不同类型的球,现在玩家X将放置与玩家Y类型的球相同的球。该过程将一直持续到游戏结束。
  • 否则,如果大球大于小球,则玩家X的得分将为M – 1,而玩家Y的得分将为N ,方法将与上述相同。在这里,玩家X首先将保持大球先开始。
  • 最后,比较两个玩家的得分,并将获胜者打印为输出。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the winner of the
// Game by arranging the balls in a row
void findWinner(int n, int m)
{
    int X = 0;
    int Y = 0;
 
    // Check if small balls are greater
    // or equal to the large ones
    if (n >= m) {
 
        // X can place balls
        // therefore scores n-1
        X = n - 1;
        Y = m;
    }
 
    // Condition if large balls
    // are greater than small
    else {
 
        // X can have m-1 as a score
        // since greater number of
        // balls can only be adjacent
        X = m - 1;
        Y = n;
    }
 
    // Compare the score
    if (X > Y)
        cout << "X";
 
    else if (Y > X)
        cout << "Y";
 
    else
        cout << "-1";
}
 
// Driver Code
int main()
{
    // Given number of small balls(N)
    // and number of large balls(M)
    int n = 3, m = 1;
 
    // Function call
    findWinner(n, m);
    return 0;
}


Java
// Java program for the above approach
class GFG{
   
// Function to find the winner of the
// Game by arranging the balls in a row
static void findWinner(int n, int m)
{
    int X = 0;
    int Y = 0;
  
    // Check if small balls are greater
    // or equal to the large ones
    if (n >= m)
    {
  
        // X can place balls
        // therefore scores n-1
        X = n - 1;
        Y = m;
    }
  
    // Condition if large balls
    // are greater than small
    else
    {
  
        // X can have m-1 as a score
        // since greater number of
        // balls can only be adjacent
        X = m - 1;
        Y = n;
    }
  
    // Compare the score
    if (X > Y)
        System.out.print("X");
  
    else if (Y > X)
        System.out.print("Y");
  
    else
        System.out.print("-1");
}
  
// Driver Code
public static void main(String[] args)
{
    // Given number of small balls(N)
    // and number of large balls(M)
    int n = 3, m = 1;
  
    // Function call
    findWinner(n, m);
}
}
 
// This code is contributed by rock_cool


Python3
# Python3 program for the above approach
 
# Function to find the winner of the
# Game by arranging the balls in a row
def findWinner(n, m):
    X = 0;
    Y = 0;
 
    # Check if small balls are greater
    # or equal to the large ones
    if (n >= m):
 
        # X can place balls
        # therefore scores n-1
        X = n - 1;
        Y = m;
 
    # Condition if large balls
    # are greater than small
    else:
 
        # X can have m-1 as a score
        # since greater number of
        # balls can only be adjacent
        X = m - 1;
        Y = n;
     
    # Compare the score
    if (X > Y):
        print("X");
 
    elif(Y > X):
        print("Y");
 
    else:
        print("-1");
 
# Driver Code
if __name__ == '__main__':
   
    # Given number of small balls(N)
    # and number of large balls(M)
    n = 3;
    m = 1;
 
    # Function call
    findWinner(n, m);
 
# This code is contributed by Rohit_ranjan


C#
// C# program for the above approach
using System;
class GFG{
   
// Function to find the winner of the
// Game by arranging the balls in a row
static void findWinner(int n, int m)
{
    int X = 0;
    int Y = 0;
  
    // Check if small balls are greater
    // or equal to the large ones
    if (n >= m)
    {
  
        // X can place balls
        // therefore scores n-1
        X = n - 1;
        Y = m;
    }
  
    // Condition if large balls
    // are greater than small
    else
    {
  
        // X can have m-1 as a score
        // since greater number of
        // balls can only be adjacent
        X = m - 1;
        Y = n;
    }
  
    // Compare the score
    if (X > Y)
        Console.Write("X");
  
    else if (Y > X)
        Console.Write("Y");
  
    else
        Console.Write("-1");
}
  
// Driver Code
public static void Main(String[] args)
{
    // Given number of small balls(N)
    // and number of large balls(M)
    int n = 3, m = 1;
  
    // Function call
    findWinner(n, m);
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
X

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