📜  计算游戏中可以减少到零或更少的数字

📅  最后修改于: 2021-04-22 08:08:46             🧑  作者: Mango

给定两个整数XY以及一个N个整数数组。玩家A可以将数组的任何元素减少X,玩家B可以将数组的任何元素增加Y。任务是计算A可以减少到0更少的元素数量。他们都在A迈出第一步的过程中发挥了无限的最佳状态。
注意:一次减少到零或更少的数字不能增加。

例子:

方法:由于游戏进行了无限长的时间,因此如果X> Y ,则打印N。现在我们需要求解X≤Y 。数字可以有两种类型:

  1. 那些在加Y时不超过X的那些表示count1 ,可以通过A减小到≤0
  2. 那些和关于添加ÿ发言权COUNT2只有其中一半可以由A减少到≤0超过X作为他们正在玩最佳和B将试图增加这些数目中的任何一个,使之成为> X在每一个轮到他了。

因此,答案将是count1 +((count2 +1)/ 2)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of numbers
int countNumbers(int a[], int n, int x, int y)
{
  
    // Base case
    if (y < x)
        return n;
  
    // Count the numbers
    int count1 = 0, count2 = 0;
    for (int i = 0; i < n; i++) {
  
        if (a[i] + y <= x)
            count1++;
        else if (a[i] <= x)
            count2++;
    }
  
    int number = (count2 + 1) / 2 + count1;
  
    return number;
}
  
// Driver Code
int main()
{
    int a[] = { 1, 2, 4, 2, 3 };
    int n = sizeof(a) / sizeof(a[0]);
  
    int x = 3, y = 3;
    cout << countNumbers(a, n, x, y);
    return 0;
}


Java
// Java implementation of the approach
  
class GFG
{
    // Function to return the count of numbers
    static int countNumbers(int a[], int n, int x, int y)
    {
      
        // Base case
        if (y < x)
            return n;
      
        // Count the numbers
        int count1 = 0, count2 = 0;
        for (int i = 0; i < n; i++) 
        {
            if (a[i] + y <= x)
                count1++;
            else if (a[i] <= x)
                count2++;
        }
        int number = (count2 + 1) / 2 + count1;
        return number;
    }
      
    // Driver Code
    public static void main(String []args)
    {
        int a[] = { 1, 2, 4, 2, 3 };
        int n = a.length;
        int x = 3, y = 3;
        System.out.println(countNumbers(a, n, x, y));    
    }
}
  
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach
  
# Function to return the count of numbers
def countNumbers( a,  n, x, y):
  
  
    # Base case
    if (y < x):
        return n
  
    # Count the numbers
    count1 = 0
    count2 = 0
    for i in range ( 0, n):
  
        if (a[i] + y <= x):
            count1 = count1 + 1
        elif (a[i] <= x):
            count2 = count2 + 1
      
  
    number = (count2 + 1) // 2 + count1
  
    return number
  
  
# Driver Code
a = [ 1, 2, 4, 2, 3 ]
n = len(a)
  
x = 3
y = 3
print(countNumbers(a, n, x, y))
  
# This code is contributed by ihritik


C#
// C# implementation of the approach
using System;
  
class GFG
{
    // Function to return the count of numbers
    static int countNumbers(int []a, int n, int x, int y)
    {
      
        // Base case
        if (y < x)
            return n;
      
        // Count the numbers
        int count1 = 0, count2 = 0;
        for (int i = 0; i < n; i++) 
        {
            if (a[i] + y <= x)
                count1++;
            else if (a[i] <= x)
                count2++;
        }
        int number = (count2 + 1) / 2 + count1;
        return number;
    }
      
    // Driver Code
    public static void Main()
    {
        int [] a = { 1, 2, 4, 2, 3 };
        int n = a.Length;
        int x = 3, y = 3;
        Console.WriteLine(countNumbers(a, n, x, y));
    }
}
  
// This code is contributed by ihritik


PHP


输出:
2