Light Bulb ZOJ - 3203-三分

  • Light Bulb

  •  ZOJ - 3203
  • 题意:求最值。
  • 思路:由于是单峰函数最值问题,不方便求解极值点可以三分极值点。
  • #include<bits/stdc++.h>
    using namespace std;
    int t;
    double h,H,D,L,R,ans,lm,rm;
    double ok(double mid)
    {
        double ret;
        ret=mid+(D*(h-mid)/(H-mid));
        return ret;
    }
    int main()
    {
        scanf("%d",&t);
        while(t--)
        {
            scanf("%lf%lf%lf",&H,&h,&D);
            L=0,R=h+0.0001;
            while(L+1e-5<R)
            {
                lm=L+(R-L)/3;
                rm=R-(R-L)/3;
                if(ok(lm)>ok(rm))R=rm;
                else L=lm;
            }
            ans=ok(L);
            printf("%.3lf\n",ans);
        }
        return 0;
    }
  •  

猜你喜欢

转载自blog.csdn.net/BePosit/article/details/85109394
ZOJ