给定一个由N 个整数组成的数组arr[] 。两名玩家,玩家 1和玩家 2 ,逐回合进行游戏,其中一名玩家可以进行以下两种移动之一:
- 将偶数数组元素转换为任何其他整数。
- 删除奇数数组元素。
不能进行任何移动的玩家输掉游戏。任务是打印游戏的获胜者。如果游戏可以永远继续,则打印-1 。
例子:
Input: arr[] = {3, 1, 9, 7}
Output: Player 2
Explanation: Since all array elements are odd, no conversion is possible.
Turn 1: Player 1 deletes 3.
Turn 2: Player 2 deletes 1.
Turn 3: Player 1 deletes 9.
Turn 4: Player 2 deletes 7. Now, Player 1 has no moves left. Therefore, Player 2 wins the game.
Input: arr[]={4, 8}
Output: -1
处理方法:按照以下步骤解决问题:
- 遍历数组。
- 计算数组中存在的偶数和奇数元素的数量。
- 如果偶数元素的数量为零,则执行以下操作:
- 如果奇数为偶数,则玩家 2将赢得比赛。
- 否则,玩家 1将赢得比赛。
- 如果奇数为奇数且数组中只有一个偶数元素,则玩家 1将赢得比赛。
- 否则,每次都会有平局。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to evaluate the
// winner of the game
void findWinner(int arr[], int N)
{
// Stores count of odd
// array elements
int odd = 0;
// Stores count of even
// array elements
int even = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// If arraay element is odd
if (arr[i] % 2 == 1) {
odd++;
}
// Otherwise
else {
even++;
}
}
// If count of even is zero
if (even == 0) {
// If count of odd is even
if (odd % 2 == 0) {
cout << "Player 2" << endl;
}
// If count of odd is odd
else if (odd % 2 == 1) {
cout << "Player 1" << endl;
}
}
// If count of odd is odd and
// count of even is one
else if (even == 1 && odd % 2 == 1) {
cout << "Player 1" << endl;
}
// Otherwise
else {
cout << -1 << endl;
}
}
// Driver Code
int main()
{
int arr[] = { 3, 1, 9, 7 };
int N = sizeof(arr)
/ sizeof(arr[0]);
findWinner(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to evaluate the
// winner of the game
static void findWinner(int arr[], int N)
{
// Stores count of odd
// array elements
int odd = 0;
// Stores count of even
// array elements
int even = 0;
// Traverse the array
for(int i = 0; i < N; i++)
{
// If arraay element is odd
if (arr[i] % 2 == 1)
{
odd++;
}
// Otherwise
else
{
even++;
}
}
// If count of even is zero
if (even == 0)
{
// If count of odd is even
if (odd % 2 == 0)
{
System.out.println("Player 2");
}
// If count of odd is odd
else if (odd % 2 == 1)
{
System.out.println("Player 1");
}
}
// If count of odd is odd and
// count of even is one
else if (even == 1 && odd % 2 == 1)
{
System.out.println("Player 1");
}
// Otherwise
else
{
System.out.println(-1);
}
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 3, 1, 9, 7 };
int N = arr.length;
findWinner(arr, N);
}
}
// This code is contributed by ipg2016107
Python3
# Python3 program for the above approach
# Function to evaluate the
# winner of the game
def findWinner(arr, N):
# Stores count of odd
# array elements
odd = 0
# Stores count of even
# array elements
even = 0
# Traverse the array
for i in range(N):
# If arraay element is odd
if (arr[i] % 2 == 1):
odd += 1
# Otherwise
else:
even += 1
# If count of even is zero
if (even == 0):
# If count of odd is even
if (odd % 2 == 0):
print("Player 2")
# If count of odd is odd
elif (odd % 2 == 1):
print("Player 1")
# If count of odd is odd and
# count of even is one
elif (even == 1 and odd % 2 == 1):
print("Player 1")
# Otherwise
else:
print(-1)
# Driver code
if __name__ == '__main__':
arr = [ 3, 1, 9, 7 ]
N = len(arr)
findWinner(arr, N)
# This code is contributed by Shivam Singh
C#
// C# program for the above approach
using System;
class GFG{
// Function to evaluate the
// winner of the game
static void findWinner(int []arr, int N)
{
// Stores count of odd
// array elements
int odd = 0;
// Stores count of even
// array elements
int even = 0;
// Traverse the array
for(int i = 0; i < N; i++)
{
// If arraay element is odd
if (arr[i] % 2 == 1)
{
odd++;
}
// Otherwise
else
{
even++;
}
}
// If count of even is zero
if (even == 0)
{
// If count of odd is even
if (odd % 2 == 0)
{
Console.WriteLine("Player 2");
}
// If count of odd is odd
else if (odd % 2 == 1)
{
Console.WriteLine("Player 1");
}
}
// If count of odd is odd and
// count of even is one
else if (even == 1 && odd % 2 == 1)
{
Console.WriteLine("Player 1");
}
// Otherwise
else
{
Console.WriteLine(-1);
}
}
// Driver Code
public static void Main()
{
int []arr = { 3, 1, 9, 7 };
int N = arr.Length;
findWinner(arr, N);
}
}
// This code is contributed by bgangwar59
Javascript
输出:
Player 2
时间复杂度: O(N)
辅助空间:O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live