题目链接:https://vjudge.net/problem/HDU-2899
三分搜索例题
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 #include <algorithm> 6 #include <string> 7 #include <cstdlib> 8 9 using namespace std; 10 11 double fx(double x,double y) 12 { 13 return 6*pow(x,7.0)+8*pow(x,6.0)+7*pow(x,3.0)+5*pow(x,2.0)-y*x; 14 } 15 16 double bottom,top,mid; 17 18 int main() 19 { 20 double y; 21 int T; 22 double liftthird,rightthird; 23 scanf("%d",&T); 24 while(T--) 25 { 26 scanf("%lf",&y); 27 bottom=0; 28 top=100; 29 while(top-bottom>1e-6) 30 { 31 liftthird=(2*bottom+top)/3; 32 rightthird=(bottom+top*2)/3; 33 if(fx(liftthird,y)<fx(rightthird,y)) top=rightthird-1e-7; 34 else bottom=liftthird-1e-7; 35 } 36 printf("%.4lf\n",fx((bottom+top)/2,y)); 37 } 38 return 0; 39 }