Hdoj 2289.Cup 题解
Posted martinlwx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hdoj 2289.Cup 题解相关的知识,希望对你有一定的参考价值。
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
- T ≤ 20.
- 1 ≤ r, R, H ≤ 100; 0 ≤ V ≤ 1000,000,000.
- r ≤ R.
- r, R, H, V are separated by ONE whitespace.
- 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
思路
高精度二分的题目,注意圆台的体积公式为(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
",l);
}
return 0;
}
以上是关于Hdoj 2289.Cup 题解的主要内容,如果未能解决你的问题,请参考以下文章