【HDU - 2899】 Strange fuction(二分或三分,求导)
生活随笔
收集整理的這篇文章主要介紹了
【HDU - 2899】 Strange fuction(二分或三分,求导)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題干:
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 200Sample 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都可以過。?
總結
以上是生活随笔為你收集整理的【HDU - 2899】 Strange fuction(二分或三分,求导)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年黄金还会涨吗?2021年黄金走
- 下一篇: 【HDU - 1266 】Reverse