给定分别为小球和大球N和M的数量,任务是通过以下两个动作来找出如果玩家X和Y都玩得最好的话哪个玩家获胜:
- 玩家X将尝试保持相同类型的球,即小球后跟另一个小球,大球后跟一个大球。
- 玩家Y将放不同类型的球,即先放小球,再放大球,反之亦然。
无法采取行动的玩家将输掉比赛。任务是找出给定球数赢得比赛的玩家。假定玩家X总是最先开始。
例子:
Input: N = 3 M = 1
Output: X
Explanation:
Since there is only 1 large ball, player X will puts it first.
Player Y puts a small ball adjacent to large one, because he can put a different type of ball adjacently.
Then player X puts a small ball (same type). Now player Y can’t keep a ball and hence cannot make a move
Sequence = {large, small, small}, hence the score would X = 2 and Y = 1.
Hence, player X wins.
Input: N = 4 M = 4
Output: Y
Explanation:
Player X first move = small, Player Y first move = large ( N = 4, M = 3 )
Player X second move = large, Player Y second move = small ( N = 3, M = 2 )
Player X third move = small, Player Y third move = large ( N = 2, M = 1 )
Player X fourth move = large, Player Y fourth move = small ( N = 1, M = 0 ).
Hence, player Y wins as player X can’t make a move.
方法:请按照以下步骤解决问题:
- 检查小球的数量是否大于或等于大球的数量,那么玩家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)