POJ1905-Expanding Rods-二分答案

一根细棒升温时会变长,在两面墙中间,会变成一个弓形。

给出变长后的长度,求新的细棒中心与没伸长时的中心的距离。

简单的数学推导后就可以二分答案了,一开始没完全掌握二分的姿势,wa了好多。而且poj double输出要用%f,用%lf就wa了。

 

#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

const double eps = 1e-5;
double n,c,L;

double func(double x)
{
    //if(fabs(x-0) < eps) return ;
    double r = (x*x+L*L/4.0)/(2*x);
    return 2*r*asin(L/(2.0*r));
}

int main()
{
    while(scanf("%lf%lf%lf",&L,&n,&c))
    {
        if(L<0 && n<0 && c<0)
            break;

        double low = 0.0,high = 0.5*L;
        double mid = (low+high)/2.0;
        double s = (1+n*c)*L;
        double ans = 0;

        while(high-low>eps)
        {
            //printf("%lf %lf %lf\n",low,mid,high);
            //printf("%lf %lf %lf\n",func(low),func(mid),func(high));

            if(func(mid) < s)
                low = mid;
            else
                high = mid;

            mid = (low+high)/2.0;
        }
        //printf("%f %f\n",mid,L);
        printf("%.3f\n",mid);
    }
}

 

posted @ 2016-01-31 19:03  Helica  阅读(233)  评论(0编辑  收藏  举报