📜  找到游戏的赢家,其中 X 选择 1,然后 Y 选择 2,然后 X 选择 3,依此类推

📅  最后修改于: 2022-05-13 01:56:10.232000             🧑  作者: Mango

找到游戏的赢家,其中 X 选择 1,然后 Y 选择 2,然后 X 选择 3,依此类推

两个玩家XY交替选择号码, X先选择。在第一回合中,X 选择 1,然后 Y 选择 2,然后 X 选择 3,游戏就这样继续进行。当玩家无法选择号码时,他输掉了比赛。给定 2 个整数AB ,分别表示XY可以选择的数字的最大总和。找到游戏的赢家。

例子:

方法:该任务可以通过维护 2 个总和来解决,一个用于X ,一个用于Y。请按照以下步骤解决问题:

  • XY最大值分别为AB。
  • 用 0 初始化 X 和 Y 的总和
  • 用 0 初始化计数器
  • 运行while循环直到 ( total_x ≤ Atotal_y ≤ B )
  • 增量计数器
  • 循环中断后,检查谁先越界

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the winner
void winner(int A, int B)
{
    // Initialize with zero
    int total_x, total_y, counter = 0;
 
    // Fixed limit
    while (total_x <= A && total_y <= B) {
        // Increment counter
        counter = counter + 1;
 
        // X's total
        total_x = total_x + counter;
 
        // Increment counter
        counter = counter + 1;
 
        // Y's total
        total_y = total_y + counter;
    }
    if (total_x > A && total_y > B)
        cout << "Y";
    else if (total_x > A)
        cout << "Y";
    else
        cout << "X";
}
 
// Driver Code
int main()
{
    int A = 3, B = 2;
    winner(A, B);
    return 0;
}


C
// C program for the above approach
#include 
 
// Function to find the winner
void winner(int A, int B)
{
    // Initialize with zero
    int total_x, total_y, counter = 0;
 
    // Fixed limit
    while (total_x <= A && total_y <= B) {
        // Increment counter
        counter = counter + 1;
 
        // X's total
        total_x = total_x + counter;
 
        // Increment counter
        counter = counter + 1;
 
        // Y's total
        total_y = total_y + counter;
    }
    if (total_x > A && total_y > B)
        printf("Y");
    else if (total_x > A)
        printf("Y");
    else
        printf("X");
}
 
// Driver Code
void main()
{
    int A = 3, B = 2;
    winner(A, B);
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to find the winner
  static void winner(int A, int B)
  {
    // Initialize with zero
    int total_x = 0, total_y = 0, counter = 0;
 
    // Fixed limit
    while (total_x <= A && total_y <= B) {
      // Increment counter
      counter = counter + 1;
 
      // X's total
      total_x = total_x + counter;
 
      // Increment counter
      counter = counter + 1;
 
      // Y's total
      total_y = total_y + counter;
    }
    if (total_x > A && total_y > B)
      System.out.println("Y");
    else if (total_x > A)
      System.out.println("Y");
    else
      System.out.println("X");
  }
 
  // Driver Code
  public static void main (String[] args) {
    int A = 3, B = 2;
    winner(A, B);
  }
}
 
// This code is contributed by hrithikgarg03188.


Python
# Python program for the above approach
 
# Function to find the winner
def winner(A, B):
     
    # Initialize with zero
    total_x = 0
    total_y = 0
    counter = 0
 
    # Fixed limit
    while (total_x <= A and total_y <= B):
         
        # Increment counter
        counter = counter + 1
 
        # X's total
        total_x = total_x + counter
 
        # Increment counter
        counter = counter + 1
 
        # Y's total
        total_y = total_y + counter
     
    if (total_x > A and total_y > B):
        print("Y")
    elif (total_x > A):
        print("Y")
    else:
        print("X")
 
# Driver Code
 
A = 3
B = 2
winner(A, B)
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# program for the above approach
using System;
class GFG {
 
  // Function to find the winner
  static void winner(int A, int B)
  {
    // Initialize with zero
    int total_x = 0, total_y = 0, counter = 0;
 
    // Fixed limit
    while (total_x <= A && total_y <= B) {
      // Increment counter
      counter = counter + 1;
 
      // X's total
      total_x = total_x + counter;
 
      // Increment counter
      counter = counter + 1;
 
      // Y's total
      total_y = total_y + counter;
    }
    if (total_x > A && total_y > B)
      Console.WriteLine("Y");
    else if (total_x > A)
      Console.WriteLine("Y");
    else
      Console.WriteLine("X");
  }
 
  // Driver Code
  public static void Main () {
    int A = 3, B = 2;
    winner(A, B);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
Y

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