I - Beavergnaw

When chomping a tree the beaver cuts a very specific shape out of the tree trunk. What is left in the tree trunk looks like two frustums of a cone joined by a cylinder with the diameter the same as its height. A very curious beaver tries not to demolish a tree but rather sort out what should be the diameter of the cylinder joining the frustums such that he chomped out certain amount of wood. You are to help him to do the calculations.

We will consider an idealized beaver chomping an idealized tree. Let us assume that the tree trunk is a cylinder of diameter D and that the beaver chomps on a segment of the trunk also of height D. What should be the diameter d of the inner cylinder such that the beaver chmped out V cubic units of wood?

Input contains multiple cases each presented on a separate line. Each line contains two integer numbers D and V separated by whitespace. D is the linear units and V is in cubic units. V will not exceed the maximum volume of wood that the beaver can chomp. A line with D=0 and V=0 follows the last case.

For each case, one line of output should be produced containing one number rounded to three fractional digits giving the value of d measured in linear units.

Sample Input
10 250
20 2500
25 7000
50 50000
0 0
Sample Output
8.054
14.775
13.115

#include<iostream>
#include<cmath>
#include<iomanip>
#define eps 1e-6
using namespace std;

//用大圆柱减去V然后求d得不到正确的答案

//参考博客https://blog.csdn.net/a1061747415/article/details/32712533  
/*
V=直径为D的圆柱的体积-两个园台的体积-直径为d的圆柱的体积。  注意是减去两个圆台。 
V=pi*(D/2)*(D/2)*D -     2/3 *( D*s1-d*s2   )     - pi*(d/2)*(d/2)*d  

V=pi*(D/2)*(D/2)*D -     2/3 *pi(   D*D/4 + d*d/4 + D*d/4   )*( (D-d)/2)     - pi*(d/2)*(d/2)*d

V=pi*D*D*D/4 -      2/3 *pi(   D*D/4 + d*d/4 + D*d/4   )*(D/2 - d/2 )     - pi*d*d*d/4

V=pi*D*D*D/4 -      2/24 *pi(   D*D + d*d + D*d   )*(D - d )     - pi*d*d*d/4

V=pi*D*D*D/4 -      2/24 *pi(   D*D *D+ d*d*D + D*d*D - D*D*d - d*d*d - D *d *d)      - pi*d*d*d/4


V=pi*D*D*D/4 -      2/24 *pi(   D*D *D - d*d*d)      - pi*d*d*d/4

V=pi*D*D*D/6 - pi*d*d*d/6

d*d*d = D*D*D - 6*V/pi

d=( D*D*D - 6*V/pi )^(1/3)


*/
//尝试了牛顿迭代法,也错了 
//需要计算 几者之间的关系。
//输入的V = 大圆柱 - 两个圆台 - 小圆柱。列出等式化简可以得到 
//#define pi 3.141592
const double pi = acos(-1.0);
int main()
{
    int d,v;
    while(cin>>d>>v)
    {
        if(d == 0&&v==0)
            break;
        /*double l = 0;
        double r = D;
        double d;
        while(r-l>eps)
        {
            d = (l+r)/2.0;
            double AVE = pi*(D/2.0)*(D/2.0)*D;
            double ave = pi*(d/2.0)*(d/2.0)*d;
//          cout<<AVE<<endl;
//          cout<<ave<<endl;
            cout<<AVE-ave<<endl;
//          AVE -= V; 
//          double d = 4*AVE/pi;
//          d = pow(d,1/3.0);
            if(AVE-ave-V>=0&&AVE-ave-V<1e-3) 
                break;
            if(AVE-ave<V)
            {
                r = d;  
            }
            else if(AVE-ave>V){
                l = d;                  
            }
        }
        */
        double D=(double)d,V=(double)v;
        double tmp=D*D*D-6*V/pi;
        printf("%.3f\n",pow(tmp,1.0/3.0));
//      cout<<fixed<<setprecision(3)<<d<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36734025/article/details/81167676
I