HDU 2289 Cup

二分,给你容器的高,上、下底半径,和水的体积,要你求水的高。

有个梯形体积公式PI * (R*R + R * r + r * r) / 3,只要把水的上底半径根据三角形相似求出来,套公式即可.

#include <iostream>
#include <cmath>

using namespace std;
const double M_PI =  acos(-1.0);
double r,R,H,V;
double f(double h)
{
    double u = r + ( R- r ) * h / H;
    return   M_PI * (r * r + u * r + u * u) * h / 3.0;
}
int main(int argc, const char *argv[])
{
    int T;

    //freopen("input.txt","r",stdin);
    cin>>T;
    while(T--)
    {
        cin>>r>>R>>H>>V;
        double low = 0.0,heigh = 100.0;
        double mid = (low+heigh)/2;
        if(V>=f(H)) 
        {
            printf("%.6lf\n",H);
            continue;
        }
        while(heigh-low>1e-10)
        {
            mid = (low + heigh)/2;
            //    printf("%.6lf %.6lf\n",mid,f(r,R,mid));    
            if(f(mid)>V)
            {
                heigh = mid;
            }else
            {
                low = mid;
            }
        }
        printf("%.6lf\n",mid);        

    }

    return 0;
}

 

posted @ 2013-09-22 17:22  Destino74  阅读(184)  评论(0编辑  收藏  举报