hdu 2899

Problem : http://acm.hdu.edu.cn/showproblem.php?pid=2899

求函数f(x)在[0,100]上的最小值

根据f'(x)在[0,100]上单调递增的性质,运用二分搜索求f'(x)在[0,100]上的零点

#include<cstdio>
double y;
double f(double x){//f(x)
    double fx=6*x;
    fx=(fx+8)*x*x*x;
    fx=(fx+7)*x;
    fx=(fx+5)*x;
    return (fx-y)*x;
}
double df(double x){//f'(x)
    double fx=42*x;
    fx=(fx+48)*x*x*x;
    fx=(fx+21)*x;
    fx=(fx+10)*x;
    return fx-y;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%lf",&y);
        double ans;
        if(df(0)>=0)ans=0;
        if(df(100)<=0)ans=100;
        if(df(0)<0 && df(100)>0){
            double L=0,R=100;
            while(R-L>1e-6){//1e-6的值为1*10^(-6) 这个值随便取小了点 
                ans=(L+R)/2;
                if(df(ans)>0)R=ans;
                else L=ans;
            }
        }
        printf("%.4lf\n",f(ans));
    }
    return 0;
}
View Code

数学课上没教过,那是老师的错.....黑黑

posted @ 2014-02-10 18:43  Cshhr  阅读(169)  评论(0编辑  收藏  举报