搜索第二题、
把函数的导函数求出来判断就行了,因为这函数是先减后增的,所以就是把mid缩小到极值点附近
1 #include <stdio.h> 2 #include <math.h> 3 const double mm = 1e-8; 4 double y; 5 double cal(double x){ 6 return 42.0*pow(x,6.0)+48.0*pow(x,5.0)+21.0*pow(x,2.0)+10.0*x; 7 } 8 double ans(double x){ 9 return 6.0*pow(x,7.0)+8.0*pow(x,6.0)+7.0*pow(x,3.0)+5.0*pow(x,2.0)-y*x; 10 } 11 int main() 12 { 13 int t;scanf("%d",&t); 14 while(t--){ 15 scanf("%lf",&y); 16 if(cal(100.0)-y<=0.0){ 17 printf("%.4lf ",ans(100.0)); 18 continue; 19 } 20 double l=0,r=100,mid; 21 while(r-l>mm){ 22 mid=(l+r)/2.0; 23 if(cal(mid)-y>0.0) r=mid; 24 else l=mid; 25 } 26 printf("%.4lf ",ans(mid)); 27 } 28 }