#include<stdio.h>
#include<math.h>
int main()
{
double a,b,c,disc,p,q;
scanf("%lf %lf %lf",&a,&b,&c);
disc=b*b-4*a*c;
if(fabs(disc)<1e-6)
printf("two equal roots: %.2lf\n",(-b)/(2*a));
else if(disc<0)
{
p=b/(-2*a);
q=sqrt(-disc)/(2*a);
printf("two fate roots :\n%.2f+%.2fi\n%.2lf-%.2lfi\n",p,q,p,q);
}
else
{
double root1,root2;
root1=(sqrt(disc)-b)/(2*a);
root2=(-sqrt(disc)-b)/(2*a);
printf("two real roots: %.2lf %.2lf\n",root1,root2);
}
return 0;
}
#include<math.h>
int main()
{
double a,b,c,disc,p,q;
scanf("%lf %lf %lf",&a,&b,&c);
disc=b*b-4*a*c;
if(fabs(disc)<1e-6)
printf("two equal roots: %.2lf\n",(-b)/(2*a));
else if(disc<0)
{
p=b/(-2*a);
q=sqrt(-disc)/(2*a);
printf("two fate roots :\n%.2f+%.2fi\n%.2lf-%.2lfi\n",p,q,p,q);
}
else
{
double root1,root2;
root1=(sqrt(disc)-b)/(2*a);
root2=(-sqrt(disc)-b)/(2*a);
printf("two real roots: %.2lf %.2lf\n",root1,root2);
}
return 0;
}
思路:判断delta的值(区分解的表达方式)->表达。
书上笔记:由于误差,判断disc是否为零时采用if disc是否小于一个极小的数(10^-6)即可。