给定第一个二次方程的值a1 , b1和c1 和第二个二次方程的值a2 , b2和c2 ,任务是查找两个二次方程式是否都具有相同的根。
例子:
Input: a1 = 1, b1 = -5, c1 = 6, a2 = 2, b2 = -10, c2 = 12
Output: Yes
Explanation:
Roots of both quadratic equations are (2, 3)
Input: a1 = 1, b1 = -5, c1 = 6, a2 = 1, b2 = -9, c2 = 20
Output: No
Explanation:
Roots of first quadratic equations are (2, 3), and Roots of second quadratic equations are (4, 5)
Therefore, both quadratic equations have differnt roots.
方法:
令两个二次方程为和
- 让我们假设给定条件为真,即两个方程都有相同的根,例如和
- 我们知道
和
其中a,b,c代表二次方程 - 所以,
对于第一二次方程式:
同样,对于第二二次方程式: - 现在,由于这两个根源是共同的,
因此,从上面的方程式 - 还,
- 结合以上等式:
- 这是两个二次方程式的两个根都必须具备的条件。
程式:
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