CUP

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
1
100 100 100 3141562
Sample Output
99.999024

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long int 
#define ull unsigned long long int 
#define e 2.718281828459
#define INF 0x7fffffff
#pragma warning(disable:4996)
#define pf printf
#define sf scanf
#define max(a,b) (a)>(b)?(a):(b);
double pi = acos(-1.0);
double eps=1e-9;
double r, R, H, v;
/*
从0到H,二分查找可h值,然后与体积比较,注意上圆面半径通过三角形相似求出带入即可
*/
bool Judge(double h,double R1) {
    return (pi * h * (R1*R1 + R1 * r + r * r) / 3 )<= v;
}


int main(void) {
    int t;

    cin >> t;
    while (t--) {
        cin >> r>> R >> H >> v;

        double left = 0.0, right = H;
        double mid;
        while (right-left>=eps) {
            mid = (left + right) / 2;

            if (Judge(mid,r+mid*(R-r)/H))
                left = mid;
            else
                right = mid;
        }
        pf("%.6lf\n", mid);

    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/jiruqianlong123/article/details/81367122
CUP