当前位置:编程学习 > C/C++ >>

POJ 1905 Expanding Rods

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;  
}  
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,