HDOJ_ACM_CUP

Problem Description
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
 
 
Code
 1 #include <stdio.h>
 2 #include <math.h>
 3 #define PI acos(-1.0)
 4 void main()
 5 {
 6     int count, i;
 7     double min, max, midh, r, R, H, V, midR, resV;
 8     scanf("%d", &count);
 9     for (i = 0; i < count; i++)
10     {
11         scanf("%lf %lf %lf %lf", &r, &R, &H, &V);
12         max = H;
13         min = 0;
14         while (max - min >= 1e-7)
15         {
16             midh = (max + min) / 2;
17             midR = R * (midh * (R - r) + r * H) / (H * (R - r) + r * H);
18             //if r is equal to R, it's cylinder 
19             if (r == R)
20                 resV = PI * R * R * midh;
21             else
22                 resV = PI / 3 * midh * (midR * midR + r * midR + r * r);
23             //dichotomy
24             if (fabs(V - resV) <= 1e-6)
25                 break;
26             else if (resV > V)
27                 max = midh;
28             else
29                 min = midh;
30         }
31         printf("%.6f\n", midh);
32     }
33 }
 以下代码,摘自其他网站
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 using namespace std;
 5 double pi = acos(-1.0);
 6 
 7 //知识点:圆台的体积公式V=pi*(r*r+r*R+R*R)*h/3
 8 int main()
 9 {
10     int t;
11     double r,R,H,V,h,h0,v0;
12     scanf("%d",&t);
13     while(t--)
14     {
15         scanf("%lf%lf%lf%lf",&r,&R,&H,&V);
16         if(r==R)
17         {
18             h = V/(pi*r*r);
19         }
20         else if(r<R)
21         {
22             h0 = H*r/(R-r);//补全的那个小圆锥的高
23             v0 = (pi*h0*r*r)/3;//补全的那个小圆锥的体积
24             h = h0*(pow((v0+V)/v0,1.0/3)-1);//公式原形:((v0+v)/v0)^(1/3) = (h+h0)/h0
25         }
26          if(h>H)h = H;
27          printf("%.6f\n",h);
28     }
29     return 0;
30 }
Key Point
Firstly, I should to emphasize that the mind is more crucial. Today I write down on the paper initially to get my ideas into shape so that I can finish in the morning.
Secondly, only when u couldn't get the exact answer directly will we use the dichotomy. For this question, I can find the  other solution from the others' blog, including the codes as above, which using the mathematics to solve. Even though I use the knowledge of math, but I just use the formula of the volume.
原文地址:https://www.cnblogs.com/chuanlong/p/2955321.html