一元二次方程的根 POJ 2702
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
int main()
{
int n,i;
float a,b,c,x1,x2,m,y1,y2;
scanf("%d",&n);
while(n--)
{
scanf("%f %f %f",&a,&b,&c);
if(b*b-4*a*c>0)
{
m=sqrt(b*b-4*a*c);
x1=(-b+m)/(2*a);
x2=(-b-m)/(2*a);
printf("x1=%.5f x2=%.5f\n",x1,x2);
}
else if(b*b-4*a*c==0)
{
m=sqrt(b*b-4*a*c);
x1=x2=(-b+m)/(2*a);
printf("x1=x2=%.5f\n",x1);
}else
{
m=sqrt(4*a*c-b*b);
x1=(-b)/(2*a);
y1=(m/(2*a));
x2=(-b)/(2*a);
y2=(m/(2*a));
printf("x1=%.5f+%.5fi x2=%.5f-%.5fi\n",x1,y1,x2,y2);
}
}
return 0;}