给定一个整数数组arr[] ,两个玩家A和B正在玩一个游戏,其中A可以从数组中删除任意数量的非零元素,这些元素是3 的倍数。类似地, B可以删除5 的倍数。不能移除任何元素的玩家输掉游戏。任务是如果A先开始并且双方都发挥最佳,则找到游戏的获胜者。
例子:
Input: arr[] = {1, 2, 3, 5, 6}
Output: A
3 and 6 are the elements that A can remove.
5 is the only element that B can remove.
A can remove 3 in his first move then B will have to remove 5. In the next turn, A will remove 6 and B will be left with no more moves to make.
Input: arr[] = {3, 5, 15, 20, 6, 9}
Output: A
方法:存放在movesA元素只被3整除的计数,在movesB元素只有5整除的数和元素双方在movesBoth整除。现在,
- 如果movesBoth = 0,那么两个玩家只能删除可以被他们各自的数字整除的元素,只有当movesA > moveB时, A才会赢得游戏。
- 如果movesBoth > 0,那么为了发挥最佳效果, A将删除所有可以被3和5整除的元素,以便B没有要从公共元素中删除的元素,那么只有当moveA + 1 时A才会获胜> 动作B
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the winner of the game
string getWinner(int arr[], int n)
{
int movesA = 0, movesB = 0, movesBoth = 0;
for (int i = 0; i < n; i++) {
// Increment common moves
if (arr[i] % 3 == 0 && arr[i] % 5 == 0)
movesBoth++;
// Increment A's moves
else if (arr[i] % 3 == 0)
movesA++;
// Increment B's moves
else if (arr[i] % 5 == 0)
movesB++;
}
// If there are no common moves
if (movesBoth == 0) {
if (movesA > movesB)
return "A";
return "B";
}
// 1 is added because A can remove all the elements
// that are part of the common moves in a single move
if (movesA + 1 > movesB)
return "A";
return "B";
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << getWinner(arr, n);
return 0;
}
Java
// Java implementation of the approach
class GfG
{
// Function to return the winner of the game
static String getWinner(int arr[], int n)
{
int movesA = 0, movesB = 0, movesBoth = 0;
for (int i = 0; i < n; i++)
{
// Increment common moves
if (arr[i] % 3 == 0 && arr[i] % 5 == 0)
movesBoth++;
// Increment A's moves
else if (arr[i] % 3 == 0)
movesA++;
// Increment B's moves
else if (arr[i] % 5 == 0)
movesB++;
}
// If there are no common moves
if (movesBoth == 0)
{
if (movesA > movesB)
return "A";
return "B";
}
// 1 is added because A can remove
// all the elements that are part
// of the common moves in a single move
if (movesA + 1 > movesB)
return "A";
return "B";
}
// Driver code
public static void main(String []args)
{
int arr[] = { 1, 2, 3, 5, 6 };
int n = arr.length;
System.out.println(getWinner(arr, n));
}
}
// This code is contributed by Rituraj Jain
Python3
# Python3 implementation of the approach
# Function to return the winner of the game
def getWinner(arr, n):
movesA, movesB, movesBoth = 0, 0, 0
for i in range(0, n):
# Increment common moves
if arr[i] % 3 == 0 and arr[i] % 5 == 0:
movesBoth += 1
# Increment A's moves
elif arr[i] % 3 == 0:
movesA += 1
# Increment B's moves
elif arr[i] % 5 == 0:
movesB += 1
# If there are no common moves
if movesBoth == 0:
if movesA > movesB:
return "A"
return "B"
# 1 is added because A can
# remove all the elements
# that are part of the common
# moves in a single move
if movesA + 1 > movesB:
return "A"
return "B"
# Driver code
if __name__ == "__main__":
arr = [1, 2, 3, 5, 6]
n = len(arr)
print(getWinner(arr, n))
# This code is contributed by Rituraj Jain
C#
// C# implementation of the approach
using System;
class GfG
{
// Function to return the winner of the game
static String getWinner(int []arr, int n)
{
int movesA = 0, movesB = 0, movesBoth = 0;
for (int i = 0; i < n; i++)
{
// Increment common moves
if (arr[i] % 3 == 0 && arr[i] % 5 == 0)
movesBoth++;
// Increment A's moves
else if (arr[i] % 3 == 0)
movesA++;
// Increment B's moves
else if (arr[i] % 5 == 0)
movesB++;
}
// If there are no common moves
if (movesBoth == 0)
{
if (movesA > movesB)
return "A";
return "B";
}
// 1 is added because A can remove
// all the elements that are part
// of the common moves in a single move
if (movesA + 1 > movesB)
return "A";
return "B";
}
// Driver code
public static void Main(String []args)
{
int []arr = { 1, 2, 3, 5, 6 };
int n = arr.Length;
Console.WriteLine(getWinner(arr, n));
}
}
// This code is contributed by
// Rajput-Ji
PHP
$movesB)
return "A";
return "B";
}
// 1 is added because A can remove all the elements
// that are part of the common moves in a single move
if ($movesA + 1 > $movesB)
return "A";
return "B";
}
// Driver code
$arr = array( 1, 2, 3, 5, 6 );
$n = sizeof($arr) / sizeof($arr[0]);
echo getWinner($arr, $n);
// This code is contributed by ajit.
?>
Javascript
输出:
A