题干:
Now, here is a fuction:
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
Sample Input
2 100 200
Sample Output
-74.4291 -178.8534
解题报告:
求二阶导数发现恒大于0,所以一阶导数是单调的,所以求一次导数后用二分法,或者不求导用三分法亦可解。
AC代码:
#include<bits/stdc++.h>using namespace std;
const double eps = 1e-6;
double y;
bool cal(double x) {double sum=0;sum = 42*(x*x*x*x*x*x) + 48*(x*x*x*x*x) + 21*(x*x) + 10*x -y; if(sum<0) return 0;else return 1;
}
double acal(double x) {double sum = 0;sum =6*(x*x*x*x*x*x*x) + 8*(x*x*x*x*x*x) + 7*(x*x*x) + 5*(x*x) -y*x ;return sum;
}
int main()
{int t;double mid,l,r;cin>>t;while(t--) {scanf("%lf",&y);l = 0;r = 100;if(cal(l) == 1) {printf("%.4f\n",acal(l));continue;}else if(cal(r) == 0) {printf("%.4f\n",acal(r));continue;}mid = (l+r)/2;while(r-l>=eps) {mid = (l+r)/2;if(cal(mid)) r = mid;else l = mid;}printf("%.4f\n",acal(mid));}return 0 ;
}
总结:
事实证明这题用精度1e-6或者1e-8都可以过。