码迷,mamicode.com
首页 > 其他好文 > 详细

Hdoj 2289.Cup 题解

时间:2018-11-16 10:30:20      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:sts   rate   cat   you   class   for   ever   strong   tput   

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

1
100 100 100 3141562

Sample Output

99.999024

Source

The 4th Baidu Cup final


思路

高精度二分的题目,注意圆台的体积公式为\(V=\frac{1}{3}\pi h(R^2+r^2+Rr)\)

不断二分查找最接近的h就好了,详见代码

代码

#include<bits/stdc++.h>
#define M_PI 3.14159265358979323846
using namespace std;
double r,R,H,V;
const double eps = 1e-8;
double cal(double h)
{
    double t = r + (R-r)*h/H;//求出h高度对应的截面的圆的半径
    return M_PI*h*(t*t + r*r + t*r)/3;
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        cin >> r >> R >> H >> V;
        double ans = 0;
        double l = 0, r = 100.0;
        double mid;
        while(r-l>=eps)
        {
            mid = (l+r)/2;
            if(cal(mid)<V)
                l = mid;
            else
                r = mid;
        }
        printf("%.6lf\n",l);
    }
    return 0;
}

Hdoj 2289.Cup 题解

标签:sts   rate   cat   you   class   for   ever   strong   tput   

原文地址:https://www.cnblogs.com/MartinLwx/p/9967435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!