📜  查找两个给定的二次方程式是否具有公共根

📅  最后修改于: 2021-04-23 21:52:56             🧑  作者: Mango

给定第一个二次方程的值a1b1c1 a_{1}x^{2} + b_{1}x + c_{1} = 0  和第二个二次方程的值a2b2c2 a_{2}x^{2} + b_{2}x + c_{2} = 0  ,任务是查找两个二次方程式是否都具有相同的根。
例子:

方法:
令两个二次方程为a_{1}x^{2} + b_{1}x + c_{1} = 0  a_{2}x^{2} + b_{2}x + c_{2} = 0

  • 让我们假设给定条件为真,即两个方程都有相同的根,例如\alpha  \beta
  • 我们知道
    \text{Sum of roots = } -\frac{b}{a}  \text{Product of roots = } \frac{c}{a}
    其中a,b,c代表二次方程ax^{2} + bx + c = 0
  • 所以,
    对于第一二次方程式:
    \text{Sum of roots = } \alpha + \beta = -\frac{b_{1}}{a_{1}}
    \text{Product of roots = } \alpha \beta = \frac{c_{1}}{a_{1}}
    同样,对于第二二次方程式:
    \text{Sum of roots = } \alpha + \beta = -\frac{b_{2}}{a_{2}}
    \text{Product of roots = } \alpha \beta = \frac{c_{2}}{a_{2}}
  • 现在,由于这两个根源是共同的,
    因此,从上面的方程式
    \alpha + \beta = -\frac{b_{1}}{a_{1}} = -\frac{b_{2}}{a_{2}}
    \Rightarrow \frac{a_{1}}{a_{2}} = \frac{b_{1}}{b_{2}}
  • 还,
    \alpha \beta = \frac{c_{1}}{a_{1}} = \frac{c_{2}}{a_{2}}
    \Rightarrow \frac{c_{1}}{c_{2}} = \frac{a_{1}}{a_{2}}
  • 结合以上等式:
  • 这是两个二次方程式的两个根都必须具备的条件

程式:

C++
// C++ Program to Find if two given
// Quadratic equations have
// common roots or not
 
#include 
using namespace std;
 
// function to check if 2 quadratic
// equations have common roots or not.
bool checkSolution(float a1, float b1,
                   float c1, float a2,
                   float b2, float c2)
{
    return (a1 / a2) == (b1 / b2)
           && (b1 / b2) == (c1 / c2);
}
 
// Driver code
int main()
{
    float a1 = 1, b1 = -5, c1 = 6;
    float a2 = 2, b2 = -10, c2 = 12;
    if (checkSolution(a1, b1, c1, a2, b2, c2))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java Program to Find if two given
// quadratic equations have common
// roots or not
class GFG {
     
// Function to check if 2 quadratic
// equations have common roots or not.
static boolean checkSolution(float a1, float b1,
                             float c1, float a2,
                             float b2, float c2)
{
    return ((a1 / a2) == (b1 / b2) &&
            (b1 / b2) == (c1 / c2));
}
     
// Driver code
public static void main (String[] args)
{
    float a1 = 1, b1 = -5, c1 = 6;
    float a2 = 2, b2 = -10, c2 = 12;
         
    if (checkSolution(a1, b1, c1, a2, b2, c2))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 program to find if two given
# quadratic equations have common 
# roots or not
 
# Function to check if 2 quadratic
# equations have common roots or not.
def checkSolution(a1, b1, c1, a2, b2, c2):
     
    return ((a1 / a2) == (b1 / b2) and
            (b1 / b2) == (c1 / c2))
 
# Driver code
a1, b1, c1 = 1, -5, 6
a2, b2, c2 = 2, -10, 12
 
if (checkSolution(a1, b1, c1, a2, b2, c2)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by divyamohan123


C#
// C# Program to Find if two given
// quadratic equations have common
// roots or not
using System;
class GFG{
     
// Function to check if 2 quadratic
// equations have common roots or not.
static bool checkSolution(float a1, float b1,
                          float c1, float a2,
                          float b2, float c2)
{
    return ((a1 / a2) == (b1 / b2) &&
            (b1 / b2) == (c1 / c2));
}
     
// Driver code
public static void Main (string[] args)
{
    float a1 = 1, b1 = -5, c1 = 6;
    float a2 = 2, b2 = -10, c2 = 12;
         
    if (checkSolution(a1, b1, c1, a2, b2, c2))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
Yes