给定5个整数,分别表示A,B,C,D和E ,它们表示三次方程 ,任务是找到该方程的积分解。如果不存在任何积分解,则打印“ NA” 。
例子:
Input: A = 1, B = 0, C = 0, D = 0, E = 27
Output: 3
Input: A = 1, B = 0, C = 0, D = 0, E = 16
Output: NA
方法:想法是使用二进制搜索。步骤如下:
- 将开始变量和结束变量分别初始化为0和10 5 。
- 查找开始和结束检查的中间(例如mid )值是否满足给定方程式。
- 如果当前中值满足给定方程式,则打印中值。
- 否则,如果f(x)的值小于E,则更新开始为mid + 1 。
- 其他更新在1月中旬结束。
- 如果我们找不到上述方程式的任何积分解,请打印“ -1” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the value at x of
// the given equation
long long int check(int A, int B, int C,
int D, long long int x)
{
long long int ans;
// Find the value equation at x
ans = (A * x * x * x
+ B * x * x
+ C * x
+ D);
// Return the value of ans
return ans;
}
// Function to find the integral
// solution of the given equation
void findSolution(int A, int B, int C,
int D, int E)
{
// Initialise start and end
int start = 0, end = 100000;
long long int mid, ans;
// Implement Binary Search
while (start <= end) {
// Find mid
mid = start + (end - start) / 2;
// Find the value of f(x) using
// current mid
ans = check(A, B, C, D, mid);
// Check if current mid satisfy
// the equation
if (ans == E) {
// Print mid and return
cout << mid << endl;
return;
}
if (ans < E)
start = mid + 1;
else
end = mid - 1;
}
// Print "NA" if not found
// any integral solution
cout << "NA";
}
// Driver Code
int main()
{
int A = 1, B = 0, C = 0;
int D = 0, E = 27;
// Function Call
findSolution(A, B, C, D, E);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the value at x of
// the given equation
static long check(int A, int B, int C,
int D, long x)
{
long ans;
// Find the value equation at x
ans = (A * x * x * x +
B * x * x + C * x + D);
// Return the value of ans
return ans;
}
// Function to find the integral
// solution of the given equation
static void findSolution(int A, int B, int C,
int D, int E)
{
// Initialise start and end
long start = 0, end = 100000;
long mid, ans;
// Implement Binary Search
while (start <= end)
{
// Find mid
mid = start + (end - start) / 2;
// Find the value of f(x) using
// current mid
ans = check(A, B, C, D, mid);
// Check if current mid satisfy
// the equation
if (ans == E)
{
// Print mid and return
System.out.println(mid);
return;
}
if (ans < E)
start = mid + 1;
else
end = mid - 1;
}
// Print "NA" if not found
// any integral solution
System.out.println("NA");
}
// Driver Code
public static void main(String args[])
{
int A = 1, B = 0, C = 0;
int D = 0, E = 27;
// Function Call
findSolution(A, B, C, D, E);
}
}
// This code is contributed by Code_Mech
Python3
# Python3 program for the above approach
# Function to find the value at x of
# the given equation
def check(A, B, C, D, x) :
ans = 0;
# Find the value equation at x
ans = (A * x * x * x +
B * x * x + C * x + D);
# Return the value of ans
return ans;
# Function to find the integral
# solution of the given equation
def findSolution(A, B, C, D, E) :
# Initialise start and end
start = 0; end = 100000;
mid = 0;
ans = 0;
# Implement Binary Search
while (start <= end) :
# Find mid
mid = start + (end - start) // 2;
# Find the value of f(x) using
# current mid
ans = check(A, B, C, D, mid);
# Check if current mid satisfy
# the equation
if (ans == E) :
# Print mid and return
print(mid);
return;
if (ans < E) :
start = mid + 1;
else :
end = mid - 1;
# Print "NA" if not found
# any integral solution
print("NA");
# Driver Code
if __name__ == "__main__" :
A = 1; B = 0; C = 0;
D = 0; E = 27;
# Function Call
findSolution(A, B, C, D, E);
# This code is contributed by AnkitRai01
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the value at x of
// the given equation
static long check(int A, int B, int C,
int D, long x)
{
long ans;
// Find the value equation at x
ans = (A * x * x * x +
B * x * x + C * x + D);
// Return the value of ans
return ans;
}
// Function to find the integral
// solution of the given equation
static void findSolution(int A, int B, int C,
int D, int E)
{
// Initialise start and end
long start = 0, end = 100000;
long mid, ans;
// Implement Binary Search
while (start <= end)
{
// Find mid
mid = start + (end - start) / 2;
// Find the value of f(x) using
// current mid
ans = check(A, B, C, D, mid);
// Check if current mid satisfy
// the equation
if (ans == E)
{
// Print mid and return
Console.WriteLine(mid);
return;
}
if (ans < E)
start = mid + 1;
else
end = mid - 1;
}
// Print "NA" if not found
// any integral solution
Console.Write("NA");
}
// Driver Code
public static void Main()
{
int A = 1, B = 0, C = 0;
int D = 0, E = 27;
// Function Call
findSolution(A, B, C, D, E);
}
}
// This code is contributed by Code_Mech
Javascript
输出:
3
时间复杂度: O(log N)