示例输出
x1=-1
x2=-5
#include<stdio.h>
#include<math.h>
int judge(double a, double b, double c)//判断一元二次方程是否有实数解
{
if(pow(b, 2)-4*a*c>0)
{
return 2;//有两个不同的实数解
}
else if(pow(b, 2)-4*a*c==0)
{
return 1;//有两个相同的实数解
}
else
{
return 0;//在实数范围内无解
}
}
float Solution(double a, double b, double c)
{
if(judge(a, b, c)>=1)//有实数解
{
double delta=pow(b, 2)-4*a*c;
double x1, x2;
x1=(-b+sqrt(delta))/(2*a);
x2=(-b-sqrt(delta))/(2*a);
if(x1>x2)
{
printf("x1=%.2f\n", x1);
printf("x2=%.2f\n", x2);
}
else
{
printf("x1=%.2f\n",x2);
printf("x2=%.2f\n",x1);
}
}
else//在实数范围内无解
{
printf("-1\n");
}
}
int main()
{
double a, b, c;
scanf("%lf%lf%lf", &a, &b, &c);
Solution(a, b, c);
return 0;
}