有两个玩家A和B,以及一堆N张牌。任务是找到游戏的获胜者,并假设两个玩家均按照以下准则发挥最佳状态:
- 玩家A总是开始游戏,而玩家随后轮流交替进行。
- 在每回合中,如果K&n = 0,则玩家可以移除K (1≤K≤N)张牌,其中n是当前筹码堆的大小。
- 如果玩家在游戏中的任何时候都无法移动,则该玩家输掉游戏,游戏结束。
例子:
Input: N = 1
Output: B
Explanation:
A can only remove 1 card, but 1 & 1 = 1, so A is unable to make a move.
Hence, B wins the game.
Input: N = 4
Output: A
Explanation:
A will remove 3 cards as 3 & 4 = 0, now only 1 card is left and B cannot make a move.
Hence, A wins the game.
方法:该想法基于以下观察结果:如果在遇到0之前, N的二进制表示形式中的1s计数为奇数,则A赢得游戏。如果在二进制字符串不存在这样的1和0的组合,则B获胜。请按照以下步骤解决问题:
- 初始化变量countOne以存储计数1 。
- 将N转换为其二进制表示形式并将其存储在字符串binString中。
- 遍历字符串binString并执行以下操作:
- 如果遇到“ 1 ”,则增加countOne 。
- 如果遇到“ 0 ”,请检查countOne是奇数还是偶数,如果countOne是奇数,则A获胜,并退出循环,否则将countOne重置为0并继续遍历。
- 如果遍历整个字符串而没有中断,则B获胜。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the winner of the
// game if both player plays optimally
void findWinner(int N)
{
// Stores the count of 1s
int onesBeforeZero = 0;
int flag = 1;
// Convert N to binary representation
int binString[32];
int i = 0;
while (N > 0)
{
// Storing remainder in binary array
binString[i] = N % 2;
N = N / 2;
i++;
}
int l = sizeof(binString) /
sizeof(binString[0]);
// Traverse the binary string
for(int j = 0; j < l; j++)
{
// If 1 is encountered,
// increment count of 1s
if (binString[j] == 1)
{
onesBeforeZero += 1;
}
// If 0 is encountered, check
// if count of 1s is odd
else
{
// If count of 1s is odd,
// then winner is A
if (onesBeforeZero & 1)
{
cout << "A";
flag = 0;
break;
}
// If count of 1s is even,
// reset it to 0
else
onesBeforeZero = 0;
}
}
// If entire loop is traversed
// without breaking, then
// B is the winner
if (flag == 1)
cout << "B";
}
// Driver Code
int main()
{
int N = 4;
// Function Call
findWinner(N);
return 0;
}
// This code is contributed by jana_sayantan
C
// C program for the above approach
#include
// Function to find the winner of the
// game if both player plays optimally
void findWinner(unsigned long long N)
{
// Stores the count of 1s
int onesBeforeZero = 0;
int flag = 1, j = 0;
char binString[32];
// Converting N into a binary string
for(int i = 31; i >= 0; i--)
{
unsigned long long temp = N >> i;
if (temp & 1)
binString[j] = '1';
else
binString[j] = '0';
j += 1;
}
// Traverse the binary string
for(int i = 0; i < 32; i++)
{
if (binString[i] == '1')
// If 1 is encountered
// increment ones count
onesBeforeZero += 1;
else
{
// If 0 is encountered check
// if ones count is odd
if (onesBeforeZero & 1)
{
// If ones count is odd
// winner is A break
printf("A");
flag = 0;
break;
}
else
// If ones count is even
// reset it to 0 and continue
onesBeforeZero = 0;
}
}
// If entire loop is traversed
// without breaking, then
// B is the winner
if (flag == 1)
printf("B");
}
// Driver code
int main()
{
unsigned long long N = 4;
// Function Call
findWinner(N);
return 0;
}
// This code is contributed by Praneeth Kapila
Java
// Java program for the above approach
class GFG{
// Function to find the winner
static void findWinner(long N)
{
// Stores the count of 1s
int onesBeforeZero = 0, flag = 1, j = 0;
String[] binString = new String[32];
// Converting N into a binary string
for(int i = 31; i >= 0; i--)
{
long temp = N >> i;
if ((temp & 1) == 1)
binString[j] = "1";
else
binString[j] = "0";
j += 1;
}
for(int i = 0; i < 32; i++)
{
if (binString[i] == "1")
// If 1 is encountered
// increment ones count
onesBeforeZero += 1;
else
{
// If 0 is encountered check
//if ones count is odd
if ((onesBeforeZero & 1) == 1)
{
// If ones count is odd winner
// is A break
System.out.println("A");
flag = 0;
break;
}
else
// If ones count is even
// reset it to 0 and continue
onesBeforeZero = 0;
}
}
// If entire loop is traversed
// without breaking, then
// B is the winner
if (flag == 1)
System.out.println("B");
}
// Driver code
public static void main(String[] args)
{
long N = 4;
// Function Call
findWinner(N);
}
}
// This code is contributed by Praneeth Kapila
Python3
# Python3 program for the above approach
# Function to find the winner of the
# game if both player plays optimally
def findWinner(N):
# Stores the count of 1s
onesBeforeZero = 0
flag = 1
# Convert N to binary representation
binString = bin(N).replace("0b", "")
l = len(binString)
# Traverse the binary string
for j in range(l):
# If 1 is encountered,
# increment count of 1s
if binString[j] == '1':
onesBeforeZero += 1
# If 0 is encountered, check
# if count of 1s is odd
else:
# If count of 1s is odd,
# then winner is A
if onesBeforeZero & 1:
print("A")
flag = 0
break
# If count of 1s is even,
# reset it to 0
else:
onesBeforeZero = 0
# If entire loop is traversed
# without breaking, then
# B is the winner
if flag == 1:
print("B")
# Driver Code
N = 4
# Function Call
findWinner(N)
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the winner
static void findWinner(long N)
{
// Stores the count of 1s
int onesBeforeZero = 0, flag = 1, j = 0;
String[] binString = new String[32];
// Converting N into a binary string
for(int i = 31; i >= 0; i--)
{
long temp = N >> i;
if ((temp & 1) == 1)
binString[j] = "1";
else
binString[j] = "0";
j += 1;
}
for(int i = 0; i < 32; i++)
{
if (binString[i] == "1")
// If 1 is encountered
// increment ones count
onesBeforeZero += 1;
else
{
// If 0 is encountered check
//if ones count is odd
if ((onesBeforeZero & 1) == 1)
{
// If ones count is odd winner
// is A break
Console.WriteLine("A");
flag = 0;
break;
}
else
// If ones count is even
// reset it to 0 and continue
onesBeforeZero = 0;
}
}
// If entire loop is traversed
// without breaking, then
// B is the winner
if (flag == 1)
Console.WriteLine("B");
}
// Driver code
public static void Main(String[] args)
{
long N = 4;
// Function Call
findWinner(N);
}
}
// This code is contributed by shivanisinghss2110
输出:
A
时间复杂度: O(log N)
辅助空间: O(log N)