二分。另外,圆台体积为v = PI*(r*r+r*R+R*R)*H/3。注意精度。
1 #include <cstdio> 2 #include <cmath> 3 4 #define exp 1e-9 5 6 const double PI = acos(-1.0); 7 8 int main() { 9 int t; 10 double r, R, H, v, lf, rt, mid, r0, nv; 11 scanf("%d", &t); 12 13 while (t--) { 14 scanf("%lf%lf%lf%lf", &r, &R, &H, &v); 15 lf = 0; 16 rt = H; 17 while (rt >= lf+exp) { 18 mid = (lf+rt)/2; 19 r0 = mid*(R-r)/H+r; 20 nv = PI*(r*r+r*r0+r0*r0)*mid/3; 21 if (fabs(nv -v) <= exp) 22 break; 23 if (nv < v) 24 lf = mid; 25 else 26 rt = mid; 27 } 28 printf("%.6lf\n", mid); 29 } 30 31 return 0; 32 }
原文地址:http://www.cnblogs.com/bombe1013/p/3855701.html