链接:http://acm.hdu.edu.cn/showproblem.php?pid=2289
细节问题:
1、r为下底,R为上底,r<=R
2、v==0直接输出"0"
3、ans>h直接输出h
4、圆台体积公式:PI*h*(r*r+r*R+R*R) / 3;
1 #include <cstdio>
2 #include <cmath>
3 #define PI (double)acos(-1.0)
4
5 int main()
6 {
7 int t;
8 double r1,r2,h,v,x,r3,vv,left,right,y;
9 scanf("%d",&t);
10 while(t--)
11 {
12 scanf("%lf%lf%lf%lf",&r1,&r2,&h,&v);
13 if(v<1e-12)
14 printf("0.000000 ");
15 else if((r2-r1)<1e-12)
16 {
17 double ans = v / (r1 * r1 * PI);
18 if(ans>h)
19 printf("%.6lf ",h);
20 else
21 printf("%.6lf ",ans);
22 }
23 else
24 {
25 left = 0; right = h;
26 while(right-left>1e-12)
27 {
28 y = (left + right) / 2;
29 r3 = r1 + (r2 - r1) * y / h;
30 vv = PI * y * (r3*r3 + r1*r1 + r1*r3) / 3;
31 if(vv>v)
32 right = y;
33 else
34 left = y;
35 }
36 printf("%.6lf ",y);
37 }
38 }
39 return 0;
40 }
2 #include <cmath>
3 #define PI (double)acos(-1.0)
4
5 int main()
6 {
7 int t;
8 double r1,r2,h,v,x,r3,vv,left,right,y;
9 scanf("%d",&t);
10 while(t--)
11 {
12 scanf("%lf%lf%lf%lf",&r1,&r2,&h,&v);
13 if(v<1e-12)
14 printf("0.000000 ");
15 else if((r2-r1)<1e-12)
16 {
17 double ans = v / (r1 * r1 * PI);
18 if(ans>h)
19 printf("%.6lf ",h);
20 else
21 printf("%.6lf ",ans);
22 }
23 else
24 {
25 left = 0; right = h;
26 while(right-left>1e-12)
27 {
28 y = (left + right) / 2;
29 r3 = r1 + (r2 - r1) * y / h;
30 vv = PI * y * (r3*r3 + r1*r1 + r1*r3) / 3;
31 if(vv>v)
32 right = y;
33 else
34 left = y;
35 }
36 printf("%.6lf ",y);
37 }
38 }
39 return 0;
40 }