69,370
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <math.h>
double root(double a, double b, double c, int i)
{
double d = sqrt(b*b - 4*a*c);
if(i == 1)
return (-b + d) / (2*a);
else
return (-b - d) / (2*a);
}
int main()
{
double a, b, c;
printf("输入方程ax^2 + bx + c = 0中的系数a b c: ");
scanf("%lf%lf%lf", &a, &b, &c);
printf("x1 = %lf, x2 = %lf\n", root(a, b, c, 1), root(a, b, c, 2));
return 0;
}
/*
运行情况:
输入方程ax^2 + bx + c = 0中的系数a b c: 1 -3 2
x1 = 2.000000, x2 = 1.000000
*/