Cup HDU 2289

The WHU ACM Team has a big cup, with which every member drinks water. Now, we know the volume of the water in the cup, can you tell us it height? 

The radius of the cup's top and bottom circle is known, the cup's height is also known.
 

Input

The input consists of several test cases. The first line of input contains an integer T, indicating the num of test cases. 
Each test case is on a single line, and it consists of four floating point numbers: r, R, H, V, representing the bottom radius, the top radius, the height and the volume of the hot water. 

Technical Specification 

1. T ≤ 20. 
2. 1 ≤ r, R, H ≤ 100; 0 ≤ V ≤ 1000,000,000. 
3. r ≤ R. 
4. r, R, H, V are separated by ONE whitespace. 
5. There is NO empty line between two neighboring cases. 

 

Output

For each test case, output the height of hot water on a single line. Please round it to six fractional digits.
 

Sample Input

1 100 100 100 3141562
 

Sample Output

99.999024
 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 const double Pi=acos(-1.0);
 5 double h0;
 6 
 7 double VV(double r,double R,double H)
 8 {
 9     return Pi*H*(R*R+r*r+R*r)/3.0;
10 }
11 
12 bool C(double r,double h,double V)
13 {
14     double R=r*(h0+h)/h0;
15     double v=Pi*h*(R*R+r*r+R*r)/3.0;
16     if(v<=V)
17         return true;
18     else
19         return false;
20 }
21 
22 bool C2(double r,double h,double V)
23 {
24     double v=r*r*Pi*h;
25     if(v<=V)
26         return true;
27     else
28         return false;
29 }
30 
31 int main()
32 {
33     int T;
34     scanf("%d",&T);
35     while(T--)
36     {
37         double r,R,H,V;
38         scanf("%lf %lf %lf %lf",&r,&R,&H,&V);
39         if(VV(r,R,H)<=V)
40             printf("%.6lf
",H);
41         else
42         {
43             double lb=0,ub=H;
44             if(R!=r)
45             {
46                 h0=r*H/(R-r);
47                 for(int i=1;i<=100;i++)
48                 {
49                     double mid=(lb+ub)/2.0;
50                     if(C(r,mid,V))
51                         lb=mid;
52                     else
53                         ub=mid;
54                 }
55                 printf("%.6lf
",lb);
56             }
57             else
58             {
59                 for(int i=1;i<=100;i++)
60                 {
61                     double mid=(lb+ub)/2.0;
62                     if(C2(r,mid,V))
63                         lb=mid;
64                     else
65                         ub=mid;
66                 }
67                 printf("%.6lf
",lb);
68             }
69             
70         }
71     }
72     return 0;
73 }
View Code
 
原文地址:https://www.cnblogs.com/cyd308/p/4693653.html