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

📅  最后修改于: 2021-09-24 04:59:36             🧑  作者: Mango

给定两个整数XY以及一个包含N 个整数的数组。玩家 A可以将数组的任何元素减少X玩家 B可以将数组的任何元素增加Y 。任务是计算A可以减少到0更少的元素数。他们都在无限时间内以最佳方式进行游戏,而 A 采取了第一步。
注意:曾经减少到零或更少的数字不能增加。
例子:

方法:由于游戏持续无限时间,我们在X > Y 时打印N。现在我们需要求解X ≤ Y 。数字可以有两种类型:

  1. 那些在添加Y 时不超过X 的人count1可以通过A减少到≤ 0
  2. 那些< X并且在添加Y 时超过X 的那些说count2只有一半可以被A减少到≤ 0因为他们正在发挥最佳作用,而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


Javascript


输出:
2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程