虽说是简单二分,但是我还是wa死了
题意:已知圆台的体积,求高度
首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3
然后以h进行二分
代码如下:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#include<math.h>
#define inf 0x7fffffff
#define eps 1e-7
#define pi acos(-1.0)
#define P system("pause")
using namespace std;
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--)
{
double r1,r2,h,v;
scanf("%lf%lf%lf%lf",&r1,&r2,&h,&v);
double h1 = 0,h2 = h,h3,v1,r;
while(1)
{
h3 = (h1 + h2)/2;
r = (r2-r1)*h3/h + r1;//按照比例求圆台上半径
v1 = pi*h3*(r1*r1+r1*r+r*r)/3;//求体积
if(fabs(v1-v) < eps) break;
if(h2 - h1 < eps) break;
if(v1 > v) h2 = h3;
if(v1 < v) h1 = h3;
}
printf("%.6lf\n",h3);
}
// P;
return 0;
}