Bridge
A suspension bridge suspends the roadway from huge main cables, which extend from one end of the bridge to the other. These cables rest on top of high towers and are secured at each end by anchorages. The towers enable the main cables to be draped over long distances.
Suppose that the maximum distance between two neighboring towers is D <tex2html_verbatim_mark>, and that the distance from the top of a tower to the roadway is H <tex2html_verbatim_mark>. Also suppose that the shape of a cable between any two neighboring towers is the same symmetric parabola (as shown in the figure). Now given B <tex2html_verbatim_mark>, the length of the bridge and L <tex2html_verbatim_mark>, the total length of the cables, you are asked to calculate the distance between the roadway and the lowest point of the cable, with minimum number of towers built (Assume that there are always two towers built at the two ends of a bridge).
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T <tex2html_verbatim_mark>(1T10) <tex2html_verbatim_mark>which is the number of test cases. T <tex2html_verbatim_mark>test cases follow, each preceded by a single blank line.
For each test case, 4 positive integers are given on a single line.
- D<tex2html_verbatim_mark>
- - the maximum distance between two neighboring towers;
- H<tex2html_verbatim_mark>
- - the distance from the top of a tower to the roadway;
- B<tex2html_verbatim_mark>
- - the length of the bridge; and
- L<tex2html_verbatim_mark>
- - the total length of the cables.
It is guaranteed that BL <tex2html_verbatim_mark>. The cable will always be above the roadway.
Output
Results should be directed to standard output. Start each case with "Case # <tex2html_verbatim_mark>:" on a single line, where # <tex2html_verbatim_mark>is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.
For each test case, print the distance between the roadway and the lowest point of the cable, as is described in the problem. The value must be accurate up to two decimal places.
Sample Input
2 20 101 400 4042 1 2 3 4
Sample Output
Case 1: 1.00 Case 2: 1.60
1 //总算遇到精度问题啦,吃一堑长一智了!!! 2 3 #include<stdio.h> 4 #include<math.h> 5 #define eps 1e-10//这题居然卡精度的啊!!!!!哭死啦!!!!1e-8都过不了啊!!! 6 7 int dcmp(double a) 8 { 9 if(fabs(a)<eps)return 0; 10 if(a>0)return 1; 11 else return -1; 12 } 13 14 int main() 15 { 16 int ca,T,i,j; 17 int cnt,B,D,H,L; 18 scanf("%d",&T); 19 for(ca=1;ca<=T;ca++) 20 { 21 double mid,d,l; 22 scanf("%d%d%d%d",&D,&H,&B,&L); 23 cnt=B/D; 24 if(cnt*D!=B) 25 cnt++; 26 d=1.0*B/cnt/2.0; 27 l=1.0*L/cnt/2.0; 28 double ll,rr; 29 ll=0.0,rr=1000000.0; 30 while(rr-ll>eps) 31 { 32 mid=(ll+rr)/2.0; 33 double tmp=sqrt(1+1.0/4.0/mid/mid/d/d); 34 double sss=mid*d*d*tmp+1.0/4.0/mid*(log(2*mid*d*(tmp+1))); 35 if(dcmp(sss-l)==1) 36 rr=mid-eps; 37 else 38 ll=mid+eps; 39 } 40 double k=mid; 41 double y=k*d*d; 42 double ans=1.0*H-y; 43 if(ans<0)ans=0;//可有可无,数据中的绳子都是伸直的!! 44 printf("Case %d: ",ca); 45 printf("%.2f ",ans); 46 if(ca<T)printf(" "); 47 } 48 }