Expanding Rods Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9197 Accepted: 2293 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 Waterloo local 2004.06.12 剛做這題的時候就意識到 這個題二分角度的時候會有精度的問題,還是沒有想到這題的精度卡的那麼死,一直都是用1e-7,也沒有意識到這個問題,就反復查代碼,是在沒法就看了看數據,發現有數據因為精度的問題結果的錯了,就開始改精度,給成1e-15,結果還是wa,很是不解,就用c++的交了一次結果過了,以後涉及精度的還是多用c++交題吧,gcc在這方面不太給力啊,不知道他是怎麼處理數據的 [cpp] #include <stdio.h> #include <string.h> #include <math.h> #define PI 3.1415927 #define EQS 1e-15 double or_l,degree,C,change_l; int main() { double binary_search(double l,double r); double res_degree,res_l; while(scanf("%lf %lf %lf",&or_l,°ree,&C)!=EOF) { if(or_l<0&°ree<0&&C<0) { break; } change_l=(1+degree*C)*or_l; res_degree=binary_search(0,180); res_l=(or_l/2)/sin(res_degree/2*PI/180)-(or_l/2)/tan(res_degree/2*PI/180); printf("%.3lf\n",res_l); } return 0; } int check(double mid) { double check_l,r; r=(or_l/2)/sin(mid/2*PI/180); check_l=2*PI*r*(mid/360); if(fabs(check_l-change_l)<=EQS) { return 0; }else if(check_l<change_l) { return 1; }else { return -1; } } double binary_search(double l,double r) { double mid; int t; while(!(fabs(r-l)<=EQS)) { mid=(l+r)/2; t=check(mid); if(t==0) { return mid; }else if(t==-1) //角度過大 { r=mid; }else if(t==1) //角度過小 { l=mid; } } return -1; }