poj 1905 (Expanding Rods) 二分的运用

来源:互联网 发布:国际运营商大数据 编辑:程序博客网 时间:2024/04/28 15:57
Expanding Rods
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 15096 Accepted: 4009

Description

When a thin rod of length L is heated n degrees, it expands(扩张) to a new length L'=(1+n*C)*L, where C is the coefficient(系数) of heat expansion(膨胀)
When a thin rod is mounted on two solid walls and then heated, it expands and takes the shape of a circular(循环的) segment(分割), the original rod being the chord(弦) of the segment. 

Your task is to compute the distance by which the center of the rod is displaced(取代)

Input

The input(投入) contains multiple lines. Each line of input contains three non-negative(非负的)numbers: the initial lenth of the rod in millimeters, the temperature change in degrees and the coefficient(系数) of heat expansion(膨胀)of the material. Input data guarantee(保证) that no rod expands(扩张) by more than one half of its original length. The last line of input contains three negative(负的) numbers and it should not be processed.

Output

For each line of input, output(输出) one line with the displacement(取代) of the center of the rod in millimeters with 3 digits(数字) ofprecision(精度)

Sample Input

1000 100 0.000115000 10 0.0000610 0 0.001-1 -1 -1

Sample Output

61.329225.0200.000
之前只知道二分能解决整数的问题,这道题用二分卡精度
#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;const double ex=1e-5;       //que保精度int main(){    double l,n,c;    while(~scanf("%lf%lf%lf",&l,&n,&c))    {         if(l==-1&&n==-1&&c==-1)break;         if(l==0||n==0||c==0)                 //当输入存在一个0时,就可以输出0.000;         {             printf("0.000\n");             continue;         }         double L=(1+n*c)*l;           //变化后的长度         double s=0,e=L*0.5;          //下界,上界         double d=0.5*l;                          double mid;         double x;         while(1)         {if(e-s<ex)break;            mid=(s+e)/2;double ll=2*atan(2*mid*d/(d*d-mid*mid))*(mid*mid+d*d)/(2*mid);       //由已知条件推出的公式if(ll<L)s=mid;else e=mid;         }        double h=mid;         printf("%.3f\n",h);    }}


0 0