HDU 2899 :(二分求最小值)

稍微麻烦点的就是需要求导判断最小值的位置

#include"cstdio"
#include"cstring"
#include"algorithm"
#include"cmath"
#define MAXN 505
using namespace std;
double cal(double x,double y){
    return (6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x);
}
double cal1(double x,double y){
    return (42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x-y);
}
int main()
{   int T;
    scanf("%d",&T);
    while(T--)
    {   double ans,y;
        scanf("%lf",&y);
        double low=0.0,high=100.0,mid;
        while(high-low>=1e-8)
        {   mid=low+(high-low)/2.0;
            if(cal1(mid,y)>=0&&cal1(low,y)<=0) high=mid;
            else if(cal1(high,y)>=0&&cal1(mid,y)<=0) low=mid;
        }
        printf("%.4lf\n",cal(mid,y));
    }
    return 0;
}

 

posted @ 2015-07-23 17:07  Septher  阅读(177)  评论(0编辑  收藏  举报