给定三个整数A 、 B和C代表二次方程Ax 2 + Bx + C = 0的系数和一个正整数K ,任务是找到二次方程的系数,其根是二次方程的根的K 倍给定的方程。
例子:
Input: A = 1, B = 2, C = 1, K = 2
Output: 1 4 4
Explanation:
The given quadratic equation x2 + 2x + 1 = 0.
Roots of the above equation are -1, -1.
Double of these roots are -2, -2.
Therefore, the quadratic equation with the roots (-2, -2) is x2 + 4x + 4 = 0.
Input: A = 1, B = -7, C = 12, K = 2
Output: 1 -14 48
方法:给定的问题可以通过使用二次根的概念来解决。请按照以下步骤解决问题:
- 设方程Ax 2 + Bx + C = 0的根分别为P和Q。
- 然后,上述方程的根的乘积由P * Q = C / A给出,上述方程的根之和由P + Q = -B / A 给出。
- 因此,所需方程的根的乘积等于:
(K * P ) * (K * Q) = K2 * P * Q = (K2 * C ) / A
- 同样,所需方程的根之和为2 * K (-B / C) 。
- 因此,所需的二次方程等于:
x2 – (Sum of the roots)x + (Product of the roots) = 0
=> Ax2 + (KB)x + (K2)C = 0
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the quadratic
// equation whose roots are K times
// the roots of the given equation
void findEquation(int A, int B, int C,
int K)
{
// Print quadratic equation
cout << A << " " << K * B
<< " " << K * K * C;
}
// Driver Code
int main()
{
int A = 1, B = 2, C = 1, K = 2;
findEquation(A, B, C, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the quadratic
// equation whose roots are K times
// the roots of the given equation
static void findEquation(int A, int B,
int C, int K)
{
// Print quadratic equation
System.out.print(A + " " + K * B +
" " + K * K * C);
}
// Driver Code
public static void main(String []args)
{
int A = 1, B = 2, C = 1, K = 2;
findEquation(A, B, C, K);
}
}
Python3
# Python3 program for the above approach
# Function to find the quadratic
# equation whose roots are K times
# the roots of the given equation
def findEquation(A, B, C, K):
# Prquadratic equation
print(A, K*B, K*K*C)
# Driver Code
if __name__ == '__main__':
A, B, C, K = 1, 2, 1, 2
findEquation(A, B, C, K)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the quadratic
// equation whose roots are K times
// the roots of the given equation
static void findEquation(int A, int B,
int C, int K)
{
// Print quadratic equation
Console.Write(A + " " + K * B +
" " + K * K * C);
}
// Driver Code
public static void Main()
{
int A = 1, B = 2, C = 1, K = 2;
findEquation(A, B, C, K);
}
}
// This code is contributed by ukasp
Javascript
输出:
1 4 4
时间复杂度: O(1)
辅助空间: O(1)