hdoj2899
Strange fuction
Time Limit : 2000/1000ms (Java/Other)???Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 14???Accepted Submission(s) : 12
Font:?Times New Roman?|?Verdana?|?Georgia
Font Size:?←?→
Problem Description
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?
這題..
首先double類型的判斷大小不能直接用==這些來判斷 可以define一個eps為1e-6
#define eps 1e-6 if(fabs(a-b)<eps)//判斷是否相等這題好像有兩種方法 暫時只寫出一種?
要求原函數(shù)的最小值 就是導(dǎo)函數(shù)為0的時候 原函數(shù)的二階導(dǎo)恒大于0 所以導(dǎo)函數(shù)是單調(diào)遞增的 某種方面可以說他是有序的一個序列 那就可以用二分查找 要繞過導(dǎo)函數(shù)為0 把算式轉(zhuǎn)換一下就是 我下面寫的ff()要等于y
ac代碼:
#include<iostream> #include<cmath> #define eps 1e-6 double y; double f(double x)//原函數(shù) {return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-x*y; } double ff(double x)//導(dǎo)函數(shù) {return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x; } int main() {using namespace std;int T,N;cin>>T;while(T--){cin>>y;double l=0,r=100,mid;while(l+eps<r){mid=(l+r)/2;if(ff(mid)<y)l=mid;elser=mid;}printf("%.4lf\n",f(mid));}return 0; }?
再放下老師的三分查找吧
/*hdu2899 三分題意:求函數(shù)F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)的最小值,y為給定的實數(shù),x連續(xù)實數(shù)且在[0,100]范圍內(nèi)思路:函數(shù)F(x)的一階導(dǎo)數(shù)F'為先負(fù)后正,二階導(dǎo)數(shù)F''>0即一階導(dǎo)數(shù)單調(diào),由此可得F(x)為單峰凹函數(shù),因此可用三分來解。 */ #include<stdio.h> double y;double f(double x) {return 6*x*x*x*x*x*x*x+8*x*x*x*x*x*x+7*x*x*x+5*x*x-y*x; }//在區(qū)間[l,r)范圍內(nèi)查找函數(shù)F(x)的凹點(最小值)----三分 double solve(double l, double r) {double eps = 1e-7;//一個非常小的常量while( l+eps <r) //當(dāng)[l,r]的區(qū)間長度非常非常小的時候,我們就認(rèn)為已經(jīng)找到答案{double lmid = l +(r-l)/3, rmid = r-(r-l)/3;if( f(lmid) <f(rmid) )//查找目標(biāo)在左半?yún)^(qū)(2/3),把右半?yún)^(qū)(1/3)丟掉r = rmid;//目標(biāo)一定在[l,rmid)else//查找目標(biāo)在右半?yún)^(qū)(2/3),把左半?yún)^(qū)(1/3)丟掉l = lmid;//目標(biāo)一定在[lmid,r)}return f(l); }int main() {int t;scanf("%d",&t);while(t--){scanf("%lf",&y);printf("%.4lf\n", solve(0,100.0) );} }?
總結(jié)
- 上一篇: android拍照保存照片方向,andr
- 下一篇: wp后台页面切换慢,打开速度慢:jque