J - 二分 POJ - 1905
When a thin rod of length L is heated n degrees, it expands to a new length L'=(1+nC)L, where C is the coefficient of heat expansion.
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
题目描述:
杆子加热会变长,由于左右有板子会拱起来。求正中间拱起的高度。
分析:
画出图,设圆心角为2a,找出几何关系。由二分找答案。
如我找出的关系:a/sin(a)=1+nxc,目标是q=tan(a/2)/2xl。
只要求出二分求出a就能得到答案q。
本来想用关系while(a/sin(a)!=(1+nxc))。因为精度较大会超时,改用for循环10000次,大概能达到答案。
代码:
#include<stdio.h> #include<math.h> const double pi=acos(-1.0); int main() { double L,N; double C; while(1){ scanf("%lf%lf%lf",&L,&N,&C); if(L==-1||N==-1||C==-1) break; double y=(1+N*C); double d; double l,r; l=0; r=pi/2; d=(l+r)/2; int c=0; //while(d/sin(d)!=y) for(int i=0;i<100000;i++) { if(y>d/sin(d)) { l=d; } else { r=d; } d=(l+r)/2; } double res=L*tan(d/2)/2; printf("%.3f\n",res); } return 0; }