Loading

HDU-2289 Cup

Cup

一个圆柱体的水壶,问体积为V的水,能装满水壶到什么高度

二分

直接二分枚举高度求解即可,计算圆台的体积:补成一个圆锥来算

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
double r, R, h, v, hh;

int dcmp(double x)
{
    return (x > eps) - (x < -eps);
}

double query(double x)
{
    double rr = r + (R - r) * x / h;
    return pi / 3 * (rr * rr * (hh + x) - r * r * hh);
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%lf%lf%lf%lf", &r, &R, &h, &v);
        if (dcmp(R - r) == 0)
        {
            double s = pi * r * r;
            printf("%.6f\n", min(v / s, h));
            continue;
        }
        hh = R * h / (R - r) - h;
        double _l = 0, _r = h;
        while (_r - _l > eps)
        {
            double mid = (_l + _r) / 2;
            double x = query(mid);
            if (x > v)
                _r = mid;
            else
                _l = mid;
        }
        printf("%.6f\n", _l);
    }
}
posted @ 2022-04-26 13:26  dgsvygd  阅读(20)  评论(0编辑  收藏  举报