给定三个数字A,B,C ,它们代表二次方程的系数(常数) ,任务是检查由这些常数表示的方程式的根是否互为倒数。
例子:
Input: A = 2, B = -5, C = 2
Output: Yes
Explanation:
The given quadratic equation is .
Its roots are (1, 1/1) which are reciprocal of each other.
Input: A = 1, B = -5, C = 6
Output: No
Explanation:
The given quadratic equation is .
Its roots are (2, 3) which are not reciprocal of each other.
方法:这个想法是使用二次根的概念来解决问题。我们可以通过以下方式制定检查一个根是否为另一个根的倒数所需的条件:
- 令方程的根为和 。
- 上式的根的乘积由下式给出: * 。
- 已知根的乘积是C / A。因此,所需条件为C = A。
下面是上述方法的实现:
C++
// C++ program to check if roots
// of a Quadratic Equation are
// reciprocal of each other or not
#include
using namespace std;
// Function to check if the roots
// of a quadratic equation are
// reciprocal of each other or not
void checkSolution(int a, int b, int c)
{
if (a == c)
cout << "Yes";
else
cout << "No";
}
// Driver code
int main()
{
int a = 2, b = 0, c = 2;
checkSolution(a, b, c);
return 0;
}
Java
// Java program to check if roots
// of a quadratic equation are
// reciprocal of each other or not
class GFG{
// Function to check if the roots
// of a quadratic equation are
// reciprocal of each other or not
static void checkSolution(int a, int b, int c)
{
if (a == c)
System.out.print("Yes");
else
System.out.print("No");
}
// Driver code
public static void main(String[] args)
{
int a = 2, b = 0, c = 2;
checkSolution(a, b, c);
}
}
// This code is contributed by shubham
Python3
# Python3 program to check if roots
# of a Quadratic Equation are
# reciprocal of each other or not
# Function to check if the roots
# of a quadratic equation are
# reciprocal of each other or not
def checkSolution(a, b, c):
if (a == c):
print("Yes");
else:
print("No");
# Driver code
a = 2; b = 0; c = 2;
checkSolution(a, b, c);
# This code is contributed by Code_Mech
C#
// C# program to check if roots
// of a quadratic equation are
// reciprocal of each other or not
using System;
class GFG{
// Function to check if the roots
// of a quadratic equation are
// reciprocal of each other or not
static void checkSolution(int a, int b, int c)
{
if (a == c)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
// Driver code
public static void Main()
{
int a = 2, b = 0, c = 2;
checkSolution(a, b, c);
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
Yes
时间复杂度: O(1)
辅助空间: O(1)