题解 | 计算一元二次方程
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include <stdio.h>
#include <math.h>
int main(void)
{
double a, b, c, d, x1, x2, t, i, n;
while (scanf("%lf %lf %lf", &a, &b, &c) != EOF)
{
d = (b * b) - (4 * a * c);
x1 = (-b + sqrt(d)) / (2 * a);
x2 = (-b - sqrt(d)) / (2 * a);
i = sqrt(-d) / (2 * a);
n = -b / (2 * a);
if (n == -0.0)
n = fabs(n);
if (x1 == -0.0)
x1 = fabs(x1);
if (x2 == -0.0)
x2 = fabs(x2);
if (a == 0)
printf("Not quadratic equation\n");
else if (d == 0)
printf("x1=x2=%.2lf\n", x1);
else if (d > 0)
{
if (x1 > x2)
{
t = x1;
x1 = x2;
x2 = t;
}
printf("x1=%.2lf;x2=%.2lf\n", x1, x2);
}
else if (d < 0)
printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi\n", n, i, n, i);
}
}
