题解 | #计算一元二次方程#
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,c;
float z;
while(scanf("%f %f %f\n",&a,&b,&c)!=EOF)
{
if(a==0)
printf("Not quadratic equation\n");
else
{
z=(b*b)-(4*a*c);
if(z==0)
{
if(b==0&&c==0)
printf("x1=x2=0.00");//防止出现这种情况时输出结果为-0.00
else
{float x=-b/(2*a);
printf("x1=x2=%0.2f\n",x);}
}
else if(z>0)
{
float x1= (-b / (2*a))-sqrt(z/(4*a*a));
float x2=(-b / (2*a))+sqrt(z/(4*a*a));
printf("x1=%0.2f;x2=%0.2f\n",x1,x2);
}
else
{
float x= -b / (2*a);
float x1=sqrt(-z) / (2*a);
printf("x1=%0.2f-%0.2fi;x2=%0.2f+%0.2fi",x,x1,x,x1);
}
}
}
return 0;
}