有两个玩家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.
方法:该想法基于以下观察:如果N的二进制表示中1的计数在遇到0之前是奇数,则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
Javascript
输出:
A
时间复杂度: O(log N)
辅助空间: O(log N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。