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