HDU 2899

来源:互联网 发布:js创建节点 编辑:程序博客网 时间:2024/06/13 19:19

一直在写二分的题目,竟然还是不知道遇到题目怎样用二分,这道题目也是看了大神的思路才知道,求导,因为我们要找的事F(X)的最小值,相当于我们数学中函数的极值问题了,那就是先判断单调了,找出极值点了,详细看代码!#include <iostream>#include <cstdio>#include <cstring>using namespace std;const double p=1e-10;//一开始这里写成了int型的,一直没有结果,睡一觉起来果断改了,就有了int y;double ff1(double x){    return 42*x*x*x*x*x*x+48*x*x*x*x*x+21*x*x+10*x-y;}double ff2(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;}double  binary_search(double left, double right){    double mid;    if(ff1(100.0)<=0)    {        return ff2(100.0);    }    else    {        while((left+p)<right)        {            mid=(left+right)/2;            if(ff1(mid)<0)            {                left=mid;            }            else            {                right=mid;            }        }        return ff2(mid);    }}int main(){    int t;    while(scanf("%d",&t)!=EOF)    {        while(t--)        {            scanf("%d",&y);            double m;            m=binary_search(0,100);            printf("%.4lf\n",m);        }    }    return 0;}


原创粉丝点击