有两个玩家P1和P2,以及两堆分别由M和N个硬币组成的硬币。在每个回合中,玩家只能从其中选择一堆,然后丢弃另一堆。该被丢弃的桩不能在游戏中进一步使用。堆球手选择将其进一步分为两堆非零部分。无法将筹码相除(即筹码中的硬币数小于2)的玩家将输掉比赛。任务是确定如果P1开始游戏并且两个玩家都发挥最佳状态,则哪个玩家获胜。
例子:
Input: M = 4, N = 4
Output: Player 1
Player 1 can choose any one of the piles as both contain the same number of coins
and then splits the chosen one (the one which is not chosen is discarded) into two piles with 1 coin each.
Now, player 2 is left with no move (as both the remaining piles contain a single coin each
which cannot be split into two groups of non-zero coins).
Input: M = 1, N = 1
Output: Player 2
There’s no move to make.
方法:只需检查其中是否有偶数个硬币即可。如果是,则玩家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