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

📅  最后修改于: 2021-09-24 04:59:20             🧑  作者: Mango

两名球员,PLAYER1player2,都打在一个给定的数字序列S一个游戏,PLAYER1开始第一和他们都发挥最佳状态。任务是找出玩家 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


Javascript


输出:
No

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程