📌  相关文章
📜  在第 i 个动作中找到捐赠 i 个糖果的游戏的获胜者

📅  最后修改于: 2021-10-26 07:00:34             🧑  作者: Mango

鉴于两个整数XY分别代表分配给玩家AB糖果,其中两个玩家在捐赠的沉迷游戏的数量糖果对手在每一个i举动。从玩家A开始,游戏交替进行,直到玩家无法捐赠所需数量的糖果并输掉游戏,任务是找到游戏的获胜者。

例子:

方法:想法是基于以下观察来解决问题:

请按照以下步骤解决问题

  1. 初始化两个变量,比如chanceAchanceB,代表玩家拥有的糖果数量减少到 0 的移动次数。
  2. 由于玩家A的糖果数量在奇数移动中减少 1,因此机会 A = 2 * (X – 1) + 1
  3. 由于玩家B的糖果数量在偶数移动中减少 1,因此机会 B = 2 * Y
  4. 如果chanceA < chanceB ,那么B将成为获胜玩家。
  5. 否则, A将成为获胜者。
  6. 打印获胜的玩家。

下面是上述方法的实现:

C++
// C++ Program for the
// above approach
 
#include 
using namespace std;
 
// Function to find the winning
// player in a game of donating i
// candies to opponent in i-th move
int stepscount(int a, int b)
{
    // Steps in which number of
    // candies of player A finishes
    int chanceA = 2 * a - 1;
 
    // Steps in which number of
    // candies of player B finishes
    int chanceB = 2 * b;
 
    // If A's candies finishes first
    if (chanceA < chanceB) {
        cout << "B";
    }
 
    // Otherwise
    else if (chanceB < chanceA) {
        cout << "A";
    }
 
    return 0;
}
 
// Driver Code
int main()
{
    // Input
 
    // Candies possessed
    // by player A
    int A = 2;
 
    // Candies possessed
    // by player B
    int B = 3;
 
    stepscount(A, B);
 
    return 0;
}


Java
// Java Program for above approach
class GFG{
     
// Function to find the winning
// player in a game of donating i
// candies to opponent in i-th move
static void stepscount(int a, int b)
{
     
    // Steps in which number of
    // candies of player A finishes
    int chanceA = 2 * a - 1;
 
    // Steps in which number of
    // candies of player B finishes
    int chanceB = 2 * b;
 
    // If A's candies finishes first
    if (chanceA < chanceB)
    {
        System.out.print("B");
    }
 
    // Otherwise
    else if (chanceB < chanceA)
    {
        System.out.print("A");
    }
}
 
// Driver code
public static void main(String[] args)
{
     
    // Input
    // Candies possessed by player A
    int A = 2;
 
    // Candies possessed by player B
    int B = 3;
 
    stepscount(A, B);
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to find the winning
# player in a game of donating i
# candies to opponent in i-th move
def stepscount(a, b):
     
    # Steps in which number of
    # candies of player A finishes
    chance_A = 2 * a - 1
     
    # Steps in which number of
    # candies of player B finishes
    chance_B = 2 * b
     
    # If A's candies finishes first
    if (chance_A < chance_B):
        return 'B'
         
    # Otherwise   
    else:
        return "A"
 
# Driver code
 
# Candies possessed by player A       
A = 2
 
# Candies possessed by player B
B = 3
 
print(stepscount(A, B))
 
# This code is contributed by abhinavjain194


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the winning
// player in a game of donating i
// candies to opponent in i-th move
static void stepscount(int a, int b)
{
     
    // Steps in which number of
    // candies of player A finishes
    int chanceA = 2 * a - 1;
 
    // Steps in which number of
    // candies of player B finishes
    int chanceB = 2 * b;
 
    // If A's candies finishes first
    if (chanceA < chanceB)
    {
        Console.Write("B");
    }
 
    // Otherwise
    else if (chanceB < chanceA)
    {
        Console.Write("A");
    }
}
 
// Driver code
static void Main()
{
     
    // Input
    // Candies possessed by player A
    int A = 2;
 
    // Candies possessed by player B
    int B = 3;
 
    stepscount(A, B);
}
}
 
// This code is contributed by abhinavjain194


Javascript


输出:
B

时间复杂度: O(1)
辅助空间: O(1)