题目链接:https://bak3.vjudge.net/problem/HDU-2199
最基础的二分搜索
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 f(double x) 12 { 13 double ans; 14 ans=8.0*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6; 15 return ans; 16 } 17 18 int main() 19 { 20 int t; 21 double y; 22 scanf("%d",&t); 23 while(t--) 24 { 25 int flag=1; 26 double left=0,mid,right=100; 27 scanf("%lf",&y); 28 while(right-left>1e-6) 29 { 30 if(y<f(left)||y>f(right)) {flag=0;break;} 31 mid=(left+right)/2.0; 32 if(f(mid)>=y) right=mid; 33 else if(f(mid)<y)left=mid; 34 } 35 if (flag) printf("%.4lf ",mid); 36 else printf("No solution! "); 37 } 38 return 0; 39 }