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