给定一个正整数不同的数组arr [] ,两个玩家A和B正在玩游戏。每次移动时,玩家都会从数组中选择两个数字x和y ,如果| x – y |在数组中不存在,则播放器将此数字添加到数组中(数组的大小增加1)。无法采取行动的玩家将输掉比赛。任务是在玩家A始终启动游戏的情况下找到游戏的获胜者。
例子:
Input: arr[] = {2, 3}
Output: A
After A’s move, array will be {2, 3, 1} and B can’t make any move.
Input: arr[] = {5, 6, 7}
Output: B
方法:在这里观察到,在游戏结束时(没有更多可移动的地方),结果数组将包含原始数组gcd的所有倍数,直至原始数组的最大元素。
For example, arr[] = {8, 10}
Since, gcd(8, 10) = 2. So the resultant array at the end of the game will contain all the multiples of 2 ≤ max(arr) i.e. 10.
Hence, arr[] = {2, 4, 6, 8, 10}
从上面的观察中,可以找到可以在原始数组上执行的移动次数,这将确定游戏的获胜者,如果移动次数是偶数,则B将成为游戏的获胜者,否则A会赢得游戏。
可以找到的移动数为(max(arr)/ gcd)– n ,其中gcd是原始数组元素的gcd, max(arr)/ gcd给出结果数组中元素的总数。从结果数组中的元素数减去元素的原始数将得出移动数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the winner of the game
char getWinner(int arr[], int n)
{
// To store the gcd of the original array
int gcd = arr[0];
// To store the maximum element
// from the original array
int maxEle = arr[0];
for (int i = 1; i < n; i++) {
gcd = __gcd(gcd, arr[i]);
maxEle = max(maxEle, arr[i]);
}
int totalMoves = (maxEle / gcd) - n;
// If number of moves are odd
if (totalMoves % 2 == 1)
return 'A';
return 'B';
}
// Driver Code
int main()
{
int arr[] = { 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << getWinner(arr, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to calculate gcd
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Function to return the winner
// of the game
static char getWinner(int []arr, int n)
{
// To store the gcd of the
// original array
int gcd = arr[0];
// To store the maximum element
// from the original array
int maxEle = arr[0];
for (int i = 1; i < n; i++)
{
gcd = __gcd(gcd, arr[i]);
maxEle = Math.max(maxEle, arr[i]);
}
int totalMoves = (maxEle / gcd) - n;
// If number of moves are odd
if (totalMoves % 2 == 1)
return 'A';
return 'B';
}
// Driver Code
public static void main(String args[])
{
int []arr = { 5, 6, 7 };
int n = arr.length;
System.out.print(getWinner(arr, n));
}
}
// This code is contributed
// by Akanksha Rai
Python3
# Python3 implementation of the approach
from math import gcd
# Function to return the winner
# of the game
def getWinner(arr, n) :
# To store the gcd of the
# original array
__gcd = arr[0];
# To store the maximum element
# from the original array
maxEle = arr[0];
for i in range(1, n) :
__gcd = gcd(__gcd, arr[i]);
maxEle = max(maxEle, arr[i]);
totalMoves = (maxEle / __gcd) - n;
# If number of moves are odd
if (totalMoves % 2 == 1) :
return 'A';
return 'B';
# Driver Code
if __name__ == "__main__" :
arr = [ 5, 6, 7 ];
n = len(arr)
print(getWinner(arr, n))
# This code is contributed by Ryuga
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to calculate gcd
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Function to return the winner
// of the game
static char getWinner(int []arr, int n)
{
// To store the gcd of the
// original array
int gcd = arr[0];
// To store the maximum element
// from the original array
int maxEle = arr[0];
for (int i = 1; i < n; i++)
{
gcd = __gcd(gcd, arr[i]);
maxEle = Math.Max(maxEle, arr[i]);
}
int totalMoves = (maxEle / gcd) - n;
// If number of moves are odd
if (totalMoves % 2 == 1)
return 'A';
return 'B';
}
// Driver Code
public static void Main()
{
int []arr = { 5, 6, 7 };
int n = arr.Length;
Console.Write(getWinner(arr, n));
}
}
// This code is contributed
// by Akanksha Rai
PHP
B
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。