二分法 poj 1905

Expanding Rods
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 8686   Accepted: 2152

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 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

Source

 

 

题意:如图,将长度为L的棒子卡在墙壁之间。现在因为某种原因,木棒变长了,因为还在墙壁之间,所以弯成了一个弧度,现在求的是弧的最高处与木棒原先的地方的最大距离。

 

解法:二分。先根据公式退出弧长的式子,接下来一步步二分,得到正确的值。

 

式子有:(S为棒子之后的弧长,r为形成这一弧长的角度,L为棒子之前的长度,h为所求的距离)

      1. S=2*Θ*r;

      2. (r-h)^2+r^2=(L/2)^2;

      3. (L/2)/r=sinΘ;

化简得:

        S=2r*asin(L/2r);

        r=(4*h^2+l^2)/8h;

其实根据以上的式子,h就可以求出来。但因为r的求法中涉及asin(),所以不好直接求。因此用二分。

即给h划定一个范围,一次次二分来假定其值,通过求出r,再求出S;与题目中给定的S比较,缩小范围,直到最后,范围之间的差值已经小于其精度,即已达到正确答案。

这里要强调的是二分的思想,求值时,不一定要直接将其求出,也可以先求出范围,再从正面缩小其范围,最后得到答案。

 

代码实现:

 

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main(){
    double n1,n2;
    double n3;
    while(cin>>n1>>n2>>n3){
           if(n1==-1&&n2==-1&&n3==-1)
                   break;
           double len=n1;
           double relen=(1+n2*n3)*n1;
           double h=len/2;
           double end=h;
           double start=0;
           while((end-start)>0.0001){
               double temp=(start+end)/2;
               double r=(4*temp*temp+len*len)/(8*temp);
               double s=2*r*asin(len/(2*r));
               if(s<relen)
                   start=temp;
               else
                   end=temp;
                   }
           printf("%.3f\n",start);
           }
    return 0;
}
    

 

posted on 2012-09-22 19:46  yumao  阅读(421)  评论(0编辑  收藏  举报

导航