分别给定小球和大球的数量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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。