POJ 1905 Expanding Rods
时间:2015-04-06 12:51:52
收藏:0
阅读:253
Expanding Rods
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 12796 | Accepted: 3299 |
Description

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
1
#include <iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> using namespace std; #define maxn 25100000 #define inf 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-7 double l,n,c; double a,r; bool ok(double a){ if(l/sin(a)*a<(1+n*c)*l)return true; else return false; } int main() { //freopen("in.txt","r",stdin); while(~scanf("%lf%lf%lf",&l,&n,&c)){ if(l==-1||n==-1||c==-1)break; double ua=0; double ub=pi/2; double mid; /* while((ub-ua)>eps){ mid=(ub+ua)/2; if(ok(mid))ua=mid; else ub=mid; } */ for(int i=0;i<100;i++){ mid=(ua+ub)/2; if(ok(mid))ua=mid; else ub=mid; } printf("%.3f\n",l/2/sin(mid)*(1-cos(mid))); } }要用while写法的话就得用下面哪种判断方法,不知道为什么,精度的问题吗?
转自http://blog.csdn.net/u013748887/article/details/24369543
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> const double esp=1e-5;//最小精度 int main() { double L,n,c,s; double h; double r; double mid; while(scanf("%lf%lf%lf",&L,&n,&c)!=EOF) { if(L==-1&&n==-1&&c==-1) break; double low=0; double high=0.5*L; double mid; s=(1+n*c)*L; while(high-low>esp)//这里不能直接用hign>low,否则当low与high很接近时会陷入死循环 { mid=(high+low)/2; r=(4*mid*mid+L*L)/(8*mid); if(2*r*asin(L/(2*r))<s) low=mid; else high=mid; } h=mid; printf("%.3lf\n",h); } return 0; }
原文:http://blog.csdn.net/u013497977/article/details/44901005
评论(0)