给定二次方程A和B的根,任务是找到方程。
注意:给定的根是整数。
例子:
Input: A = 2, B = 3
Output: x^2 – (5x) + (6) = 0
x2 – 5x + 6 = 0
x2 -3x -2x + 6 = 0
x(x – 3) – 2(x – 3) = 0
(x – 3) (x – 2) = 0
x = 2, 3
Input: A = 5, B = 10
Output: x^2 – (15x) + (50) = 0
方法:如果二次方程ax 2 + bx + c = 0的根是A和B,则它知道
A + B = – b / a和A * B = c * a 。
现在,ax 2 + bx + c = 0可以写成
x 2 +(b / a)x +(c / a)= 0(因为a!= 0)
x 2 –(A + B)x +(A * B)= 0,[因为,A + B = -b * a和A * B = c * a]
即x 2 –(根的总和)x +根的乘积= 0
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to find the quadratic
// equation whose roots are a and b
void findEquation(int a, int b)
{
int sum = (a + b);
int product = (a * b);
cout << "x^2 - (" << sum << "x) + ("
<< product << ") = 0";
}
// Driver code
int main()
{
int a = 2, b = 3;
findEquation(a, b);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to find the quadratic
// equation whose roots are a and b
static void findEquation(int a, int b)
{
int sum = (a + b);
int product = (a * b);
System.out.println("x^2 - (" + sum +
"x) + (" + product + ") = 0");
}
// Driver code
public static void main(String args[])
{
int a = 2, b = 3;
findEquation(a, b);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
# Function to find the quadratic
# equation whose roots are a and b
def findEquation(a, b):
summ = (a + b)
product = (a * b)
print("x^2 - (", summ,
"x) + (", product, ") = 0")
# Driver code
a = 2
b = 3
findEquation(a, b)
# This code is contributed by Mohit Kumar
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to find the quadratic
// equation whose roots are a and b
static void findEquation(int a, int b)
{
int sum = (a + b);
int product = (a * b);
Console.WriteLine("x^2 - (" + sum +
"x) + (" + product + ") = 0");
}
// Driver code
public static void Main()
{
int a = 2, b = 3;
findEquation(a, b);
}
}
// This code is contributed by CodeMech.
Javascript
输出:
x^2 - (5x) + (6) = 0