Expanding Rods
Time Limit: 1000MS | Memory Limit: 30000K | |
Total 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 (取代).
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
(数字) of
precision
(精度).
Sample Input
1000 100 0.0001 15000 10 0.00006 10 0 0.001 -1 -1 -1
Sample Output
61.329 225.020 0.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);
}
}