检查二次方程的一根是否是另一根的两倍
给定三个数字 A,B,C ,代表一个二次方程的系数(常数),任务是检查由这些常数代表的方程的一个根是否是另一个的两倍。 示例:
输入: A = 1,B = -3,C = 2 输出:是 说明: 给定的二次方程是 它的根是(1,2)。 输入: A = 1,B = -5,C = 6 输出: No 解释: 给定的二次方程是 它的根是(2,3)。或
做法:思路是用二次根的概念来解决问题。我们可以通过: 来表达检查一个根是否是另一个根的两倍所需的条件
- 根之和= + = 3 。该值等于:
- 同样,根的乘积= * = 2 。该值等于:
- 我们可以解上面两个方程和得到条件:
- 因此,为了使根的第一个假设成立,上述条件需要成立。因此,对于给定的系数,我们简单地检查上述条件是否成立。
以下是上述方法的实现:
C++
// C++ program to check if one root
// of a Quadratic Equation is
// twice of other or not
#include <iostream>
using namespace std;
// Function to find the required answer
void checkSolution(int a, int b, int c)
{
if (2 * b * b == 9 * a * c)
cout << "Yes";
else
cout << "No";
}
// Driver code
int main()
{
int a = 1, b = 3, c = 2;
checkSolution(a, b, c);
return 0;
}
Java 语言(一种计算机语言,尤用于创建网站)
// Java program to check if one root
// of a quadratic equation is
// twice of other or not
class GFG{
// Function to find the required answer
static void checkSolution(int a, int b, int c)
{
if (2 * b * b == 9 * a * c)
System.out.print("Yes");
else
System.out.print("No");
}
// Driver Code
public static void main(String[] args)
{
int a = 1, b = 3, c = 2;
checkSolution(a, b, c);
}
}
// This code is contributed by shubham
Python 3
# Python3 program to check if one root
# of a Quadratic Equation is
# twice of other or not
# Function to find the required answer
def checkSolution(a, b, c):
if (2 * b * b == 9 * a * c):
print("Yes");
else:
print("No");
# Driver code
a = 1; b = 3; c = 2;
checkSolution(a, b, c);
# This code is contributed by Code_Mech
C
java 描述语言
Output:
Yes
时间复杂度: O(1)
辅助空间: O(1)
版权属于:月萌API www.moonapi.com,转载请注明出处