题解 | #计算一元二次方程#
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include <stdio.h>
#include <math.h>
int main() {
float a,b,c,der,x,x1,x2;
while((scanf("%f %f %f",&a,&b,&c))!= EOF)
{
der = b*b-4*a*c;
if(a==0.0)
{
printf("Not quadratic equation");
}
else
{
if(der == 0)
{
x1 = (-b)/(2*a);
x2 = (-b)/(2*a);
if(x1 == -0.0)
{
x1 = 0.0;
printf("x1=x2=%.2f\n",x1);
}
else
{
printf("x1=x2=%.2f\n",x1);
}
}
else if(der > 0)
{
x1 = (-b+sqrtf(der))/(2*a);
x2 = (-b-sqrtf(der))/(2*a);
if(x1<=x2)
{
printf("x1=%.2f;x2=%.2f\n",x1,x2);
}
else
{
x = x1;
x1 = x2;
x2 = x;
printf("x1=%.2f;x2=%.2f\n",x1,x2);
}
}
else
{
x = sqrtf(4*a*c-b*b)/(2*a);
x1 = (-b)/(2*a);
x2 = (-b)/(2*a);
printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n",x1,x,x2,x);
}
}
}
return 0;
}
查看9道真题和解析