avatar
fireworks99
keep hungry keep foolish

HDU 2289 Cup

Description

一个杯子,已知上下两底面半径和杯子高度、水的体积,求水在杯中的高度

题目链接

二分:

越看越玄乎了…

那些个能直接解出来的模拟题、数学题,直接解出来的答案是错误的,或是精度丧失了的,而二分查找(枚举)赋值代数是:natural,纯真自然的、一尘不染的,可AC的…

Code

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
double r, R, H, V;

double cal(double ans)
{
    double tr = ans / H * (R - r) + r;
    return (tr * tr + r * r + r * tr) * PI * ans / 3;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%lf%lf%lf%lf", &r, &R, &H, &V);
//        if(R - r < eps)
//            printf("%.6f\n", V / (r * r * PI));
//        else
//        {
//            double left = V / (R * R * PI);
//            double right = V / (r * r * PI);
            double left = 0, right = 100, tv, mid;
            while(right - left > eps)
            {
                mid = (left + right) / 2;
                tv = cal(mid);
                if(fabs(tv - V) < eps)
                    break;
                else if(tv < V)
                    left = mid;
                else
                    right = mid;
            }
            printf("%.6f\n", mid);
//        }
    }
    return 0;
}

有时候人为好心地去缩小界线——反而好心办坏儿事…

Site by Baole Zhao | Powered by Hexo | theme PreciousJoy