给定一个由N 个数字组成的数组。两个玩家X和Y玩一个游戏,其中每一步都有一个玩家选择一个数字。一个号码只能选择一次。选择完所有数字后,如果X和Y收集的数字之和的绝对差能被4整除,则玩家X获胜,否则Y获胜。
注意:玩家 X 开始游戏,并在每一步都优化选择数字。
例子:
Input: a[] = {4, 8, 12, 16}
Output: X
X chooses 4
Y chooses 12
X chooses 8
Y chooses 16
|(4 + 8) – (12 + 16)| = |12 – 28| = 16 which is divisible by 4.
Hence, X wins
Input: a[] = {7, 9, 1}
Output: Y
方法:可以按照以下步骤解决问题:
- 将count0 、 count1 、 count2和count3初始化为0 。
- 如果a[i] % 4 == 0 , a[i] % 4 == 1 , a[i] % 4 == 2或a[i] % 4 则迭代数组中的每个数字并相应地增加上述计数器== 3 。
- 如果count0 、 count1 、 count2和count3都是偶数,则X获胜,否则Y获胜。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to decide the winner
int decideWinner(int a[], int n)
{
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
// Iterate for all numbers in the array
for (int i = 0; i < n; i++) {
// Condition to count
// If mod gives 0
if (a[i] % 4 == 0)
count0++;
// If mod gives 1
else if (a[i] % 4 == 1)
count1++;
// If mod gives 2
else if (a[i] % 4 == 2)
count2++;
// If mod gives 3
else if (a[i] % 4 == 3)
count3++;
}
// Check the winning condition for X
if (count0 % 2 == 0
&& count1 % 2 == 0
&& count2 % 2 == 0
&& count3 == 0)
return 1;
else
return 2;
}
// Driver code
int main()
{
int a[] = { 4, 8, 5, 9 };
int n = sizeof(a) / sizeof(a[0]);
if (decideWinner(a, n) == 1)
cout << "X wins";
else
cout << "Y wins";
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to decide the winner
static int decideWinner(int []a, int n)
{
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
// Iterate for all numbers in the array
for (int i = 0; i < n; i++)
{
// Condition to count
// If mod gives 0
if (a[i] % 4 == 0)
count0++;
// If mod gives 1
else if (a[i] % 4 == 1)
count1++;
// If mod gives 2
else if (a[i] % 4 == 2)
count2++;
// If mod gives 3
else if (a[i] % 4 == 3)
count3++;
}
// Check the winning condition for X
if (count0 % 2 == 0 && count1 % 2 == 0 &&
count2 % 2 == 0 && count3 == 0)
return 1;
else
return 2;
}
// Driver code
public static void main(String args[])
{
int []a = { 4, 8, 5, 9 };
int n = a.length;
if (decideWinner(a, n) == 1)
System.out.print("X wins");
else
System.out.print("Y wins");
}
}
// This code is contributed by Akanksha Rai
Python3
# Python3 implementation of the approach
# Function to decide the winner
def decideWinner(a, n):
count0 = 0
count1 = 0
count2 = 0
count3 = 0
# Iterate for all numbers in the array
for i in range(n):
# Condition to count
# If mod gives 0
if (a[i] % 4 == 0):
count0 += 1
# If mod gives 1
elif (a[i] % 4 == 1):
count1 += 1
# If mod gives 2
elif (a[i] % 4 == 2):
count2 += 1
# If mod gives 3
elif (a[i] % 4 == 3):
count3 += 1
# Check the winning condition for X
if (count0 % 2 == 0 and count1 % 2 == 0 and
count2 % 2 == 0 and count3 == 0):
return 1
else:
return 2
# Driver code
a = [4, 8, 5, 9]
n = len(a)
if (decideWinner(a, n) == 1):
print("X wins")
else:
print("Y wins")
# This code is contributed by mohit kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to decide the winner
static int decideWinner(int []a, int n)
{
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
// Iterate for all numbers in the array
for (int i = 0; i < n; i++)
{
// Condition to count
// If mod gives 0
if (a[i] % 4 == 0)
count0++;
// If mod gives 1
else if (a[i] % 4 == 1)
count1++;
// If mod gives 2
else if (a[i] % 4 == 2)
count2++;
// If mod gives 3
else if (a[i] % 4 == 3)
count3++;
}
// Check the winning condition for X
if (count0 % 2 == 0 && count1 % 2 == 0 &&
count2 % 2 == 0 && count3 == 0)
return 1;
else
return 2;
}
// Driver code
public static void Main()
{
int []a = { 4, 8, 5, 9 };
int n = a.Length;
if (decideWinner(a, n) == 1)
Console.Write("X wins");
else
Console.Write("Y wins");
}
}
// This code is contributed by Akanksha Rai
PHP
Javascript
输出:
X wins
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。