Description
解一元二次方程ax2+bx+c=0的解。保证有解
Input
a,b,c的值。
Output
两个根X1和X2,其中X1>=X2。 结果保留两位小数。
Sample
Input
Output
Hint
提示:计算过程中,分母是(2*a) - #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- double f(double a, double b, double c){
- double x1;
- x1 = ((-b) + sqrt(b * b - 4 * a * c)) / (2 * a);
- return x1;
- }
- double h(double a, double b, double c){
- double x2;
- x2 = ((-b) - sqrt(b * b - 4 * a * c)) / (2 * a);
- return x2;
- }
- int main()
- {
- double a, b, c, x1, x2, t;
- scanf("%lf %lf %lf", &a, &b, &c);
- x1 = f(a, b, c);
- x2 = h(a, b, c);
- if(x1 < x2){
- t = x1;
- x1 = x2;
- x2 = t;
- }
- printf("%.2f %.2f\n", x1, x2);
- return 0;
- }
复制代码
|