📜  生成具有根的总和和乘积的二次方程

📅  最后修改于: 2021-10-26 06:22:35             🧑  作者: Mango

给定两个整数SM ,任务是找到二次方程的系数,使得根的和和乘积分别为SM。

例子:

方法:给定的问题可以通过使用二次方程的性质来解决,如下所示:

从以上两个等式中,如果a 的值为1,b 的值为(-1)*S ,而c 的值为P 。因此,方程由下式给出:

X^2 + (-1)*S*X + P = 0

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the quadratic
// equation from the given sum and
// products of roots
void findEquation(int S, int M)
{
    // Print the coefficients
    cout << "1 " << (-1) * S << " "
         << M << endl;
}
 
// Driver Code
int main()
{
    int S = 5, M = 6;
    findEquation(S, M);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
   
// Function to find the quadratic
// equation from the given sum and
// products of roots
public static void findEquation(int S, int M)
{
     
    // Print the coefficients
    System.out.println("1 " + ((-1) * S) + " " + M);   
}
 
// Driver code
public static void main(String[] args)
{
      int S = 5, M = 6;
       
    findEquation(S, M);
}
}
 
// This code is contributed by user_qa7r


Python3
# Python3 program for the above approach
 
# Function to find the quadratic
# equation from the given sum and
# products of roots
def findEquation(S, M):
     
    # Print the coefficients
    print("1 ", ((-1) * S), " " , M)
 
# Driver Code
S = 5
M = 6
 
findEquation(S, M)
 
# This code is contributed by Ankita saini


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the quadratic
// equation from the given sum and
// products of roots
public static void findEquation(int S, int M)
{
     
    // Print the coefficients
    Console.Write("1 " + ((-1) * S) + " " + M);  
}
 
// Driver code
static void Main()
{
    int S = 5, M = 6;
        
    findEquation(S, M);
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
1 -5 6

时间复杂度: O(1)
辅助空间: O(1)