📌  相关文章
📜  求出最小正整数x使得a(x ^ 2)+ b(x)+ c> = k

📅  最后修改于: 2021-04-22 06:21:41             🧑  作者: Mango

给定四个整数abck 。任务是找到x的最小正值,使得ax 2 + bx + c≥k

例子:

方法:想法是使用二进制搜索。我们的搜索下限为0,因为x必须是最小的正整数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the minimum positive
// integer satisfying the given equation
int MinimumX(int a, int b, int c, int k)
{
    int x = INT_MAX;
  
    if (k <= c)
        return 0;
  
    int h = k - c;
    int l = 0;
  
    // Binary search to find the value of x
    while (l <= h) {
        int m = (l + h) / 2;
        if ((a * m * m) + (b * m) > (k - c)) {
            x = min(x, m);
            h = m - 1;
        }
        else if ((a * m * m) + (b * m) < (k - c))
            l = m + 1;
        else
            return m;
    }
  
    // Return the answer
    return x;
}
  
// Driver code
int main()
{
    int a = 3, b = 2, c = 4, k = 15;
    cout << MinimumX(a, b, c, k);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
      
// Function to return the minimum positive
// integer satisfying the given equation
static int MinimumX(int a, int b, int c, int k)
{
    int x = Integer.MAX_VALUE;
  
    if (k <= c)
        return 0;
  
    int h = k - c;
    int l = 0;
  
    // Binary search to find the value of x
    while (l <= h) 
    {
        int m = (l + h) / 2;
        if ((a * m * m) + (b * m) > (k - c)) 
        {
            x = Math.min(x, m);
            h = m - 1;
        }
        else if ((a * m * m) + (b * m) < (k - c))
            l = m + 1;
        else
            return m;
    }
  
    // Return the answer
    return x;
}
  
// Driver code
public static void main(String[] args)
{
    int a = 3, b = 2, c = 4, k = 15;
    System.out.println(MinimumX(a, b, c, k));
}
}
  
// This code is contributed by Code_Mech.


Python3
# Python3 implementation of the approach
  
# Function to return the minimum positive
# integer satisfying the given equation
def MinimumX(a, b, c, k):
  
    x = 10**9
  
    if (k <= c):
        return 0
  
    h = k - c
    l = 0
  
    # Binary search to find the value of x
    while (l <= h):
        m = (l + h) // 2
        if ((a * m * m) + (b * m) > (k - c)):
            x = min(x, m)
            h = m - 1
  
        elif ((a * m * m) + (b * m) < (k - c)):
            l = m + 1
        else:
            return m
  
    # Return the answer
    return x
  
# Driver code
a, b, c, k = 3, 2, 4, 15
print(MinimumX(a, b, c, k))
  
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to return the minimum positive
// integer satisfying the given equation
static int MinimumX(int a, int b, int c, int k)
{
    int x = int.MaxValue;
  
    if (k <= c)
        return 0;
  
    int h = k - c;
    int l = 0;
  
    // Binary search to find the value of x
    while (l <= h) 
    {
        int m = (l + h) / 2;
        if ((a * m * m) + (b * m) > (k - c)) 
        {
            x = Math.Min(x, m);
            h = m - 1;
        }
        else if ((a * m * m) + (b * m) < (k - c))
            l = m + 1;
        else
            return m;
    }
  
    // Return the answer
    return x;
}
  
// Driver code
public static void Main()
{
    int a = 3, b = 2, c = 4, k = 15;
    Console.Write(MinimumX(a, b, c, k));
}
}
  
// This code is contributed by Akanksha Rai


PHP
 ($k - $c))
        { 
            $x = min($x, $m); 
            $h = $m - 1; 
        } 
        else if (($a * $m * $m) + 
                 ($b * $m) < ($k - $c)) 
            $l = $m + 1; 
        else
            return $m; 
    } 
  
    // Return the answer 
    return $x; 
} 
  
// Driver code 
$a = 3; $b = 2; $c = 4; $k = 15; 
  
echo MinimumX($a, $b, $c, $k);
  
// This code is contributed by Ryuga
?>


输出:
2