z
1 #include <stdio.h> 2 3 double f(double *p, double x); 4 int main(void) 5 { 6 7 double an[4]; 8 double a, b; 9 for (int i = 3; i >= 0; i--) 10 { 11 scanf("%lf", &an[i]); 12 } 13 14 scanf("%lf %lf", &a, &b); 15 16 int flag = 0; //标志位 17 while (b - a > 1e-3) 18 { 19 double m = (a + b) / 2; 20 double ret_m = f(an, m); 21 if (ret_m == 0) //判断浮点数等于0 22 { 23 printf("%.2f ", m); 24 flag = 1; 25 break; 26 } 27 28 double ret_a = f(an, a); 29 double ret_b = f(an, b); 30 31 if (ret_a * ret_m > 0) //判断同号 32 { 33 a = m; 34 } 35 if (ret_b * ret_m > 0) 36 { 37 b = m; 38 } 39 } 40 41 if (flag == 0) 42 { 43 printf("%.2f ", (a + b) / 2); 44 } 45 return 0; 46 } 47 double f(double *p, double x) 48 { 49 double ret; 50 51 ret = p[3] * x * x * x + p[2] * x * x + p[1] * x + p[0]; 52 53 return ret; 54 }