📌  相关文章
📜  通过选择数字,根据总和的绝对差来预测游戏的获胜者

📅  最后修改于: 2021-04-24 21:33:39             🧑  作者: Mango

给定一个N个数字的数组。两名玩家XY玩游戏,其中每一步一个玩家选择一个数字。一个号码只能选择一次。选择完所有数字后,如果XY收集的数字之和之间的绝对差可被4整除,则玩家X获胜,否则Y获胜。
注意:玩家X开始游戏,并且在每个步骤中均会选择最佳数字。

例子:

方法:可以按照以下步骤解决问题:

  • count0count1count2count3初始化为0
  • 如果a [i]%4 == 0a [i]%4 == 1a [i]%4 == 2a [i]%4 ,则对数组中的每个数字进行迭代并相应地增加上述计数器== 3
  • 如果count0count1count2count3均为偶数,则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


输出:
X wins