hdu-2289-Cup

来源:互联网 发布:圆形进度条js代码 编辑:程序博客网 时间:2024/06/05 04:20

经典二分

点击打开链接看原题


Problem Description
The WHU ACM Team has a big cup, with which every member drinks water. Now, we know the volume of the water in the cup, can you tell us it height?

The radius of the cup's top and bottom circle is known, the cup's height is also known.
 
Input
The input consists of several test cases. The first line of input contains an integer T, indicating the num of test cases.
Each test case is on a single line, and it consists of four floating point numbers: r, R, H, V, representing the bottom radius, the top radius, the height and the volume of the hot water.

Technical Specification

1. T ≤ 20.
2. 1 ≤ r, R, H ≤ 100; 0 ≤ V ≤ 1000,000,000.
3. r ≤ R.
4. r, R, H, V are separated by ONE whitespace.
5. There is NO empty line between two neighboring cases.

Output
For each test case, output the height of hot water on a single line. Please round it to six fractional digits.
 

Sample Input
1100 100 100 3141562
 

Sample Output
99.999024代码
#include <cstdio>#include <iostream>#include <cmath>#include <cstring>#include <cstdlib>#include <string>#include <queue>#include <vector>#include <stack>#include <algorithm>#define esp 1.0e-10#define PI acos(-1.0)using namespace std;int T;double r,R,H,V,ri,l,mid;double Vo(double x){    double u=(R-r)*x/H+r;    return (pow(r,2)+pow(u,2)+r*u)/3*PI*x;}int main (){   while ( scanf ("%d",&T)!=EOF)   {        while (T--)        {            scanf ("%lf %lf %lf %lf",&r,&R,&H,&V);            double l=0,ri=R;            if(V==0)            {                printf ("%.6lf\n",0);                continue;            }            while(ri-l>esp)            {                mid=(l+ri)/2;                if(Vo(mid)>V)                    ri=mid;                else                    l=mid;            }            printf("%.6lf\n",(l+ri)/2);        }   }   return 0;}

方法:
这里就是先确定圆台的体积公式V=1/3πH(R^2+r^2+R*r);我居然还可以用微积分求出这个公式,尴尬啊。
然后就是找到H和r的一个关系式,利用相似三角形。

0 0
原创粉丝点击