给定一个整数N表示一支笔中的盒子数量,两个玩家P1和P2按照以下规则在他们之间进行分配N支笔的游戏:
- P1通过拿2 个X笔迈出了第一步。 (最初, X = 0)
- P2需要3 个X笔。
- X 的值在每次移动后增加 1。
- P1和P2交替移动。
- 如果当前玩家必须拿走的笔数超过盒子中剩余的笔数,则他们退出。
- 当两个玩家都退出或盒子变空时,游戏就结束了。
游戏结束后打印以下详细信息的任务:
- 盒子中剩余的笔数。
- P1收集的笔数。
- P2收集的笔数。
例子:
Input: N = 22
Output:
Number of pens remaining in the box: 14
Number of pens collected by P1 : 5
Number of pens collected by P2 : 3
Explanation:
- Move 1: X = 0, P1 takes 1 pen from the box. Therefore, N = 22 – 1 = 21.
- Move 2: X = 1, P2 takes 3 pens from the box. Therefore, N = 21 – 3 = 18.
- Move 3: X = 2, P1 takes 4 pens from the box. Therefore, N = 18 – 4 = 14.
- Move 4: X = 3, P2 quits as 27 > 14.
- Move 5: X = 4, P1 quits as 16 > 14.
- Game Over! Both players have quit.
Input: N = 1
Output:
Number of pens remaining in the box : 0
Number of pens collected by P1 : 1
Number of pens collected by P2 : 0
方法:想法是使用递归。请按照以下步骤解决问题:
1. 定义一个递归函数:
Game_Move(N, P1, P2, X, Move, QuitP1, QuitP2)
where,
N : Total number of Pens
P1 : Score of P1
P2 : Score of P2
X : Initialized to zero
Move = 0 : P1’s turn
Move = 1 : P2’s turn
QuitP1 : Has P1 quit
QuitP2 : Has P2 quit
2. 最后,在游戏结束后打印最终值
下面是上述方法的实现:
C++
// C++ implementation of the
// above approach
#include
using namespace std;
// N = Total number of Pens
// P1 : Score of P1
// P2 : Score of P2
// X : Initialized to zero
// Move = 0 : P1's turn
// Move = 1 : P2's turn
// QuitP1 : Has P1 quit
// QuitP2 : Has P2 quit
// Recursive function to play Game
void solve(int& N, int& P1, int& P2, int& X, bool Move,
bool QuitP1, bool QuitP2)
{
if (N == 0 or (QuitP1 and QuitP2)) {
// Box is empty, Game Over! or
// Both have quit, Game Over!
cout << "Number of pens remaining"
<< " in the box: " << N << endl;
cout << "Number of pens collected"
<< " by P1: " << P1 << endl;
cout << "Number of pens collected"
<< " by P2: " << P2 << endl;
return;
}
if (Move == 0 and QuitP1 == false) {
// P1 moves
int req_P1 = pow(2, X);
if (req_P1 <= N) {
P1 += req_P1;
N -= req_P1;
}
else {
QuitP1 = true;
}
}
else if (Move == 1 and QuitP2 == false) {
// P2 moves
int req_P2 = pow(3, X);
if (req_P2 <= N) {
P2 += req_P2;
N -= req_P2;
}
else {
QuitP2 = true;
}
}
// Increment X
X++;
// Switch moves between P1 and P2
Move = ((Move == 1) ? 0 : 1);
solve(N, P1, P2, X, Move, QuitP1, QuitP2);
}
// Function to find the number of
// pens remaining in the box and
// calculate score for each player
void PenGame(int N)
{
// Score of P1
int P1 = 0;
// Score of P2
int P2 = 0;
// Initialized to zero
int X = 0;
// Move = 0, P1's turn
// Move = 1, P2's turn
bool Move = 0;
// Has P1 quit
bool QuitP1 = 0;
// Has P2 quit
bool QuitP2 = 0;
// Recursively continue the game
solve(N, P1, P2, X, Move,
QuitP1, QuitP2);
}
// Driver Code
int main()
{
int N = 22;
PenGame(N);
return 0;
}
Java
// Java implementation of the
// above approach
import java.util.*;
import java.lang.*;
public class GFG
{
// N = Total number of Pens
// P1 : Score of P1
// P2 : Score of P2
// X : Initialized to zero
// Move = 0 : P1's turn
// Move = 1 : P2's turn
// QuitP1 : Has P1 quit
// QuitP2 : Has P2 quit
// Recursive function to play Game
static void solve(int N, int P1, int P2, int X,
int Move, boolean QuitP1, boolean QuitP2)
{
if (N == 0 || (QuitP1 && QuitP2))
{
// Box is empty, Game Over! or
// Both have quit, Game Over!
System.out.println("Number of pens remaining"
+ " in the box: " + N);
System.out.println("Number of pens collected"
+ " by P1: " + P1);
System.out.println("Number of pens collected"
+ " by P2: " + P2);
return;
}
if (Move == 0 && QuitP1 == false)
{
// P1 moves
int req_P1 = (int)(Math.pow(2, X));
if (req_P1 <= N)
{
P1 += req_P1;
N -= req_P1;
}
else
{
QuitP1 = true;
}
}
else if (Move == 1 && QuitP2 == false)
{
// P2 moves
int req_P2 = (int)(Math.pow(3, X));
if (req_P2 <= N)
{
P2 += req_P2;
N -= req_P2;
}
else
{
QuitP2 = true;
}
}
// Increment X
X++;
// Switch moves between P1 and P2
Move = ((Move == 1) ? 0 : 1);
solve(N, P1, P2, X, Move, QuitP1, QuitP2);
}
// Function to find the number of
// pens remaining in the box and
// calculate score for each player
static void PenGame(int N)
{
// Score of P1
int P1 = 0;
// Score of P2
int P2 = 0;
// Initialized to zero
int X = 0;
// Move = 0, P1's turn
// Move = 1, P2's turn
int Move = 0;
// Has P1 quit
boolean QuitP1 = false;
// Has P2 quit
boolean QuitP2 = false;
// Recursively continue the game
solve(N, P1, P2, X, Move, QuitP1, QuitP2);
}
// Driver Code
public static void main (String[] args)
{
int N = 22;
PenGame(N);
}
}
// This code is contributed by jana_sayantan.
Python3
# Python3 implementation of the
# above approach
# N = Total number of Pens
# P1 : Score of P1
# P2 : Score of P2
# X : Initialized to zero
# Move = 0 : P1's turn
# Move = 1 : P2's turn
# QuitP1 : Has P1 quit
# QuitP2 : Has P2 quit
# Recursive function to play Game
def solve(N, P1, P2, X, Move,
QuitP1, QuitP2):
if (N == 0 or (QuitP1 and QuitP2)):
# Box is empty, Game Over! or
# Both have quit, Game Over!
print("Number of pens remaining in the box: ", N)
print("Number of pens collected by P1: ", P1)
print("Number of pens collected by P2: ", P2)
return
if (Move == 0 and QuitP1 == False):
# P1 moves
req_P1 = int(pow(2, X))
if (req_P1 <= N):
P1 += req_P1
N -= req_P1
else:
QuitP1 = True
elif (Move == 1 and QuitP2 == False):
# P2 moves
req_P2 = int(pow(3, X))
if (req_P2 <= N):
P2 += req_P2
N -= req_P2
else:
QuitP2 = True
# Increment X
X += 1
# Switch moves between P1 and P2
if(Move == 1):
Move = 0
else:
Move = 1
solve(N, P1, P2, X, Move, QuitP1, QuitP2)
# Function to find the number of
# pens remaining in the box and
# calculate score for each player
def PenGame(N):
# Score of P1
P1 = 0
# Score of P2
P2 = 0
# Initialized to zero
X = 0
# Move = 0, P1's turn
# Move = 1, P2's turn
Move = False
# Has P1 quit
QuitP1 = False
# Has P2 quit
QuitP2 = False
# Recursively continue the game
solve(N, P1, P2, X, Move,
QuitP1, QuitP2)
# Driver Code
N = 22
PenGame(N)
# This code is contributed by Dharanendra L V.
C#
// C# implementation of the
// above approach
using System;
class GFG {
// N = Total number of Pens
// P1 : Score of P1
// P2 : Score of P2
// X : Initialized to zero
// Move = 0 : P1's turn
// Move = 1 : P2's turn
// QuitP1 : Has P1 quit
// QuitP2 : Has P2 quit
// Recursive function to play Game
static void solve(int N, int P1, int P2, int X,
int Move, bool QuitP1, bool QuitP2)
{
if (N == 0 || (QuitP1 && QuitP2)) {
// Box is empty, Game Over! or
// Both have quit, Game Over!
Console.WriteLine("Number of pens remaining"
+ " in the box: " + N);
Console.WriteLine("Number of pens collected"
+ " by P1: " + P1);
Console.WriteLine("Number of pens collected"
+ " by P2: " + P2);
return;
}
if (Move == 0 && QuitP1 == false) {
// P1 moves
int req_P1 = (int)(Math.Pow(2, X));
if (req_P1 <= N) {
P1 += req_P1;
N -= req_P1;
}
else {
QuitP1 = true;
}
}
else if (Move == 1 && QuitP2 == false)
{
// P2 moves
int req_P2 = (int)(Math.Pow(3, X));
if (req_P2 <= N)
{
P2 += req_P2;
N -= req_P2;
}
else
{
QuitP2 = true;
}
}
// Increment X
X++;
// Switch moves between P1 and P2
Move = ((Move == 1) ? 0 : 1);
solve(N, P1, P2, X, Move, QuitP1, QuitP2);
}
// Function to find the number of
// pens remaining in the box and
// calculate score for each player
static void PenGame(int N)
{
// Score of P1
int P1 = 0;
// Score of P2
int P2 = 0;
// Initialized to zero
int X = 0;
// Move = 0, P1's turn
// Move = 1, P2's turn
int Move = 0;
// Has P1 quit
bool QuitP1 = false;
// Has P2 quit
bool QuitP2 = false;
// Recursively continue the game
solve(N, P1, P2, X, Move, QuitP1, QuitP2);
}
// Driver Code
public static void Main()
{
int N = 22;
PenGame(N);
}
}
// This code is contributed by chitranayal.
Javascript
Number of pens remaining in the box: 14
Number of pens collected by P1: 5
Number of pens collected by P2: 3
时间复杂度: O(Log(N))
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。