当前位置: 首页 > 工具软件 > CUP > 使用案例 >

(二分)cup

太叔飞翰
2023-12-01

点击打开题目链接

AC代码:

#include <iostream>
#include <cmath>
#include<cstdio>

using namespace std;
const double p = acos(-1.0);
int main()
{
    double r1, r2, h, v;
    int t;
    cin >> t;
    double h1, h2;
    double mid;
    while (t--)
    {
        cin >> r1 >> r2 >> h >> v;
        h1 = 0;
        h2 = h + 1e-6;
        while (h2 - h1 >= 1e-6)
        {
            mid = (h1 + h2) / 2;
        /*    if (abs(p * mid * (r1 * r1 + r1 * ((r1+r2)/h * mid) + ((r1+r2)/h * mid) * ((r1+r2)/h * mid)) / 3 - v) <= 1e-6)
            {
                break;
            }*/
            double tmp=p * mid * (r1 * r1 + r1 * ((r2-r1)/h * mid+r1) + ((r2-r1)/h * mid+r1) *((r2-r1)/h * mid+r1)) / 3;
            if (tmp <= v)
            {
                h1 = mid;
            }
            else
            {
                h2 = mid;
            }
           // printf("%f %f\n",h1,h2);
            //getchar();
        }
         printf("%.6f\n", h1);
    }
    return 0;
}


 类似资料: