Cup-hd2289·

 
 
题意:给出圆台的底面半径,顶部半径,高还有水的体积,求水的高度
思路:我们可以直到水的高度必然在0到最大高度之间,在这个区间内进行二分即可

圆台体积公式
  • formula
公式描述:
公式中r为上底半径、R为下底半径、h为高。
发现如果直接使用公式进行就计算,不能通过,无语,只能使用二分了
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define PI acos(-1.0)
#define exp 1e-9
double solve(double r,double R,double h,double H)
{
    double u = h/H*(R-r) + r;
    return PI/3*(r*r+r*u+u*u)*h;
}
int main()
{
    int t;
    double r,R,H,V,mid,vv,f,l;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf%lf%lf%lf",&r,&R,&H,&V);
        f=0;
        l=100;
        while(l-f>exp)
        {
            mid=(l+f)/2;
            vv=solve(r,R,mid,H);
            if(fabs(vv-V)<=exp)
                break;
            else if(vv>V)
                l=mid-exp;
            else
                f=mid+exp;
        }
        printf("%.6lf\n",mid);
    }
    return 0;
}

发布了37 篇原创文章 · 获赞 12 · 访问量 6545

猜你喜欢

转载自blog.csdn.net/weixin_38960774/article/details/79391477
HD
CUP
今日推荐