题解 | #牛牛的计划#
计算一元二次方程
http://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
没什么想说的,只想感慨一下:做出来的那一刻心态都快崩了!
#include <math.h>
int main()
{
float a = 0, b = 0, c = 0;
float derTa = 0;
while(scanf("%f %f %f", &a, &b, &c) != EOF)
{
if(a == 0)
{
printf("Not quadratic equation\n");
}
else
{
derTa = b*b - 4.0*a*c;
if(derTa == 0)
{
float x = (-1.0*b) / (2.0*a);
if(!x)
{
printf("x1=x2=%.2f\n", x+0);
}
else
{
printf("x1=x2=%.2f\n",x);
}
}
else if(derTa > 0)
{
derTa = sqrt(1.0*b*b - 4.0*a*c);
float x1 = (-1.0*b - derTa) / (2.0*a);
float x2 = (-1.0*b + derTa) / (2.0*a);
printf("x1=%.2f;x2=%.2f\n", x1, x2);
}
else if(derTa < 0)
{
float shiBu = (-1.0*b) / (2*a);
float xuBu = sqrt(-1.0 * derTa) / (2*a);
if(shiBu)
{
printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n",
shiBu, xuBu, shiBu, xuBu);
}
else if(!shiBu)
{
printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n",
-1.0*shiBu, xuBu, -1.0*shiBu, xuBu);
}
}
}
}
return 0;
}