📜  预测硬币游戏的赢家

📅  最后修改于: 2021-09-24 05:03:46             🧑  作者: Mango

有两个玩家P1P2以及分别由MN 个硬币组成的两堆硬币。在每一回合,玩家只能从这些堆中选择一个并丢弃另一个。这个丢弃的堆不能在游戏中进一步使用。玩家选择的堆被进一步分成两堆非零部分。不能分堆的玩家,即堆中的硬币数量<2,输掉游戏。任务是确定如果P1开始游戏并且两个玩家都以最佳方式进行游戏,则确定哪个玩家获胜。
例子:

方法:只需检查堆中是否有偶数个硬币。如果是,则玩家 1 获胜,否则玩家 2 获胜。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print the winner of the game
void findWinner(int M, int N)
{
    if (M % 2 == 0 || N % 2 == 0)
        cout << "Player 1";
    else
        cout << "Player 2";
}
 
// Driver code
int main()
{
    int M = 1, N = 2;
    findWinner(M, N);
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG {
 
    // Function to print the winner of the game
    static void findWinner(int M, int N)
    {
        if (M % 2 == 0 || N % 2 == 0)
            System.out.println("Player 1");
        else
            System.out.println("Player 2");
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int M = 1, N = 2;
        findWinner(M, N);
    }
}
 
// This code is contributed by ajit.


Python3
# Python implementation of the approach
# Function to print the winner of the game
  
def findWinner(M, N):
    if (M % 2 == 0 or N % 2 == 0):
        print("Player 1");
    else:
        print("Player 2");
  
# Driver code
M = 1;
N = 2;
findWinner(M, N);
 
 
# This code contributed by PrinciRaj1992


C#
// C# implementation of the approach
using System;
 
class GFG {
 
    // Function to print the winner of the game
    static void findWinner(int M, int N)
    {
        if (M % 2 == 0 || N % 2 == 0)
            Console.WriteLine("Player 1");
        else
            Console.WriteLine("Player 2");
    }
 
    // Driver code
    static public void Main()
    {
        int M = 1, N = 2;
        findWinner(M, N);
    }
}
 
// This code is contributed by Tushil..


PHP


Javascript


输出:
Player 1