📌  相关文章
📜  两人游戏,玩家可以删除所有出现的数字

📅  最后修改于: 2021-05-08 18:45:31             🧑  作者: Mango

两个玩家player1player2正在以给定的编号序列S进行游戏,其中玩家1首先开始,并且他们两个都玩得很尽兴。任务是确定玩家1是赢还是输。如果他赢了,则打印“是”,否则打印“否”。
游戏规则如下:

  • 玩家的交替回合。
  • 在每个回合中,当前玩家必须选择当前序列S中的一个或多个元素,以使所有选定元素的值都相同,并从S中删除这些元素。
  • 当玩家无法选择任何东西时(序列S已经为空),该玩家将输掉游戏。

例子:

方法:可以给出上述问题的方法,就好像顺序中的元素总数是偶数,而且出现多次的元素数也是偶数时,第一个玩家就无法获胜。如果出现不止一次的元素数量为奇数,并且重复出现的元素的数量大于或等于4且是2的倍数,则第一个玩家无法获胜。否则, player1可以成为赢家。

下面是上述方法的实现:

C++
// C++ implementation for Two player
// game in which a player can remove
// all occurrences of a number
 
#include 
using namespace std;
 
// Function that print whether
// player1 can wins or loses
void game(int v[], int n)
{
    unordered_map m;
 
    // storing the number of occurrence
    // of elements in unordered map
    for (int i = 0; i < n; i++) {
 
        if (m.find(v[i]) == m.end())
            m[v[i]] = 1;
 
        else
            m[v[i]]++;
    }
 
    int count = 0;
 
    // variable to check if the
// occurrence of repeated
// elements is >= 4 and
    // multiple of 2 or not
    int check = 0;
 
    // count elements which
// occur more than once
    for (auto i: m) {
        if (i.second > 1) {
 
            if (i.second >= 4
&& i.second % 2 == 0)
                check++;
 
            count++;
        }
    }
 
    if (check % 2 != 0)
        bool flag = false;
 
    if (check % 2 != 0)
        cout << "Yes" << endl;
 
    else if (n % 2 == 0
&& count % 2 == 0)
        cout << "No" << endl;
 
    else
        cout << "Yes" << endl;
}
 
// Driver code
int main()
{
 
    int arr[] = { 3, 2, 2, 3, 3, 5 };
 
    int size = sizeof(arr)
/ sizeof(arr[0]);
 
    game(arr, size);
 
    return 0;
}


Java
// Java implementation for Two player
// game in which a player can remove
// all occurrences of a number
import java.util.*;
class GFG{
     
// Function that print whether
// player1 can wins or loses
public static void game(int[] v,
                        int n)
{
  HashMap m = new HashMap<>();
 
  // Storing the number of occurrence
  // of elements in unordered map
  for (int i = 0; i < n; i++)
  {
    if (!m.containsKey(v[i]))
      m.put(v[i], 1);
    else
      m.replace(v[i], m.get(v[i]) + 1);
  }
 
  int count = 0;
 
  // variable to check if the
  // occurrence of repeated
  // elements is >= 4 and
  // multiple of 2 or not
  int check = 0;
 
  // count elements which
  // occur more than once
  for (Map.Entry i : m.entrySet())
  {
    if(i.getValue() > 1)
    {
      if(i.getValue() >= 4 &&
         i.getValue() % 2 == 0)
        check++;
 
      count++;
    }
  }
   
  boolean flag;
   
  if (check % 2 != 0)
    flag = false;
  if (check % 2 != 0)
    System.out.println("Yes");
  else if (n % 2 == 0  &&
           count % 2 == 0)
    System.out.println("No");
  else
    System.out.println("Yes");
}
 
public static void main(String[] args)
{
  int[] arr = {3, 2, 2, 3, 3, 5};
  int size = arr.length;
  game(arr, size);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 implementation for two player
# game in which a player can remove
# all occurrences of a number
from collections import defaultdict
 
# Function that print whether
# player1 can wins or loses
def game(v, n):
 
    m = defaultdict(int)
     
    # Storing the number of occurrence
    # of elements in unordered map
    for i in range(n):
        if (v[i] not in m):
            m[v[i]] = 1
             
        else:
            m[v[i]] += 1
             
    count = 0
 
    # Variable to check if the
    # occurrence of repeated
    # elements is >= 4 and
    # multiple of 2 or not
    check = 0
 
    # Count elements which
    # occur more than once
    for i in m.values():
        if (i > 1):
            if (i >= 4 and i % 2 == 0):
                check += 1
            count += 1
 
    if (check % 2 != 0):
        flag = False
 
    if (check % 2 != 0):
        print("Yes")
 
    elif (n % 2 == 0 and count % 2 == 0):
        print("No")
         
    else:
        print("Yes")
 
# Driver code
if __name__ == "__main__":
     
    arr = [ 3, 2, 2, 3, 3, 5 ]
    size = len(arr)
     
    game(arr, size)
 
# This code is contributed by chitranayal


C#
// C# implementation for Two player
// game in which a player can remove
// all occurrences of a number
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function that print whether
// player1 can wins or loses
public static void game(int[] v,
                        int n)
{
    Dictionary m = new Dictionary();
     
    // Storing the number of occurrence
    // of elements in unordered map
    for(int i = 0; i < n; i++)
    {
        if (!m.ContainsKey(v[i]))
            m.Add(v[i], 1);
        else
            m[v[i]]= m[v[i]] + 1;
    }
     
    int count = 0;
     
    // Variable to check if the
    // occurrence of repeated
    // elements is >= 4 and
    // multiple of 2 or not
    int check = 0;
     
    // Count elements which
    // occur more than once
    foreach(KeyValuePair i in m)
    {
        if (i.Value > 1)
        {
            if (i.Value >= 4 &&
                i.Value % 2 == 0)
                check++;
             
            count++;
        }
    }
     
    bool flag;
     
    if (check % 2 != 0)
        flag = false;
    if (check % 2 != 0)
        Console.WriteLine("Yes");
    else if (n % 2 == 0 &&
         count % 2 == 0)
        Console.WriteLine("No");
    else
        Console.WriteLine("Yes");
}
 
// Driver code
public static void Main(String[] args)
{
    int[] arr = { 3, 2, 2, 3, 3, 5 };
    int size = arr.Length;
     
    game(arr, size);
}
}
 
// This code is contributed by Amit Katiyar


输出:
No








时间复杂度:上述方法的复杂度为O(N)。