利用公式x1=−b+b2−4ac√2a,x2=−b−b2−4ac√2ax1=−b+b2−4ac2a,x2=−b−b2−4ac2a,求一元二次方程ax2+bx+c=0ax2+bx+c=0的根,其中a不等于0。结果要求精确到小数点后5位。
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
double a,b,c,x1,x2;
cin>>a>>b>>c;
if ((a==0)||(b*b-4*a*c<0)) cout<<"No answer!";
else
{
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
if (x1==x2) printf("x1=x2=%.5lf",x1);
else if (x1<x2) printf("x1=%.5lf;x2=%.5lf",x1,x2);
else printf("x1=%.5lf;x2=%.5lf",x2,x1);
}
return 0;
}