📜  不使用Shridharacharya公式的a + b + c = 0时二次方程的根

📅  最后修改于: 2021-04-27 21:11:34             🧑  作者: Mango

给定三个整数abc ,使得a + b + c = 0 。任务是找到二次方程ax 2 + bx + c = 0的根
例子:

方法:a + b + c = 0时,方程ax 2 + bx + c = 0的根总是1c / a
例如,

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print the roots of the
// quadratic equation when a + b + c = 0
void printRoots(long a, long b, long c)
{
    cout << 1 << ", " << c / (a * 1.0);
}
 
// Driver code
int main()
{
    long a = 2;
    long b = 3;
    long c = -5;
    printRoots(a, b, c);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
    // Function to print the roots of the
    // quadratic equation when a + b + c = 0
    static void printRoots(long a, long b, long c)
    {
        System.out.println(1 + ", " + c / (a * 1.0));
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        long a = 2;
        long b = 3;
        long c = -5;
        printRoots(a, b, c);
    }
}
 
// This code is contributed by
// sanjeev2552


Python3
# Python3 implementation of the approach
 
# Function to print the roots of the
# quadratic equation when a + b + c = 0
def printRoots(a, b, c):
    print(1, ",", c / (a * 1.0))
 
# Driver code
a = 2
b = 3
c = -5
printRoots(a, b, c)
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to print the roots of the
// quadratic equation when a + b + c = 0
static void printRoots(long a, long b, long c)
{
    Console.WriteLine("1, " + c / (a * 1.0));
}
 
// Driver code
public static void Main()
{
    long a = 2;
    long b = 3;
    long c = -5;
    printRoots(a, b, c);
}
}
 
// This code is contributed by Nidhi


PHP


Javascript


输出:
1, -2.5

时间复杂度: O(1)