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

📅  最后修改于: 2021-09-24 03:20:42             🧑  作者: 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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程