HDU 2289 Cup【高精度,二分】

简介: Cup Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8942    Accepted Submission(s): 2744 Pro...

Cup

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8942    Accepted Submission(s): 2744


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
 

 

Recommend
分析:二分,对高度进行二分,然后我们知道圆台的公式为V=pi/3*(r*r+u*u+r*u)*h;然后可二分解决!这题精度比较坑爹,所以稍微注意点就好了!
下面给出AC代码:
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 inline int read()
 5 {
 6     int x=0,f=1;
 7     char ch=getchar();
 8     while(ch<'0'||ch>'9')
 9     {
10         if(ch=='-')
11             f=-1;
12         ch=getchar();
13     }
14     while(ch>='0'&&ch<='9')
15     {
16         x=x*10+ch-'0';
17         ch=getchar();
18     }
19     return x*f;
20 }
21 inline void write(int x)
22 {
23     if(x<0)
24     {
25         putchar('-');
26         x=-x;
27     }
28     if(x>9)
29     {
30         write(x/10);
31     }
32     putchar(x%10+'0');
33 }
34 const double pi=acos(-1.0);
35 const double eps=1e-9;
36 double r,R,h,H,V;
37 inline double check(double r,double R,double h,double H)
38 {
39     double u=h/H*(R-r)+r;
40     return pi*1.0*(r*r+u*u+u*r)*h/3;
41 }
42 int main()
43 {
44     int n;
45     n=read();
46     while(n--)
47     {
48         scanf("%lf%lf%lf%lf",&r,&R,&H,&V);
49         double L1=0.0;
50         double R1=100.0;
51         double mid;
52         while(L1+eps<R1)
53         {
54             mid=(L1+R1)/2;
55             double vv=check(r,R,mid,H);
56             if(fabs(vv-V)<=eps)
57                 break;
58             else if(vv>V)
59                 R1=mid-eps;
60             else L1=mid+eps;
61         }
62         printf("%.6lf\n",mid);
63     }
64     return 0;
65 }

 

目录
相关文章
|
4月前
|
算法
【每日一题Day363】LC275H 指数Ⅱ | 二分答案
【每日一题Day363】LC275H 指数Ⅱ | 二分答案
33 0
|
4月前
|
人工智能 BI
【每日一题Day354】LC2316统计无向图中无法互相到达点对数 | 并查集
【每日一题Day354】LC2316统计无向图中无法互相到达点对数 | 并查集
25 0
|
6月前
|
算法 C++
剑指offer(C++)-JZ70:矩形覆盖(算法-动态规划)
剑指offer(C++)-JZ70:矩形覆盖(算法-动态规划)
|
7月前
|
缓存 算法 Java
【手绘算法】力扣 1 两数之和(Two Sum)
Hi,大家好,我是Tom。一个美术生转到Java开发的程序员。从今天开始,我将带大家每天刷一道题。我会用手绘的方式给大家讲解解题思路。在解题过程中,也会手写一些伪代码。当然,如果想要完整的源码的话,可以到我的个人主页简介中获取。 今天给大家分享的是力扣启蒙题第1题,求两数之和。虽然很简单,但是它的通过率只有52%。
64 0
|
6月前
|
机器学习/深度学习 算法 C++
剑指offer(C++)-JZ61:扑克牌顺子(算法-模拟)
剑指offer(C++)-JZ61:扑克牌顺子(算法-模拟)
|
6月前
|
机器学习/深度学习
华为机试HJ53:杨辉三角的变形
华为机试HJ53:杨辉三角的变形
|
6月前
|
人工智能 算法 测试技术
华为机试HJ52:计算字符串的距离(动态规划)
华为机试HJ52:计算字符串的距离(动态规划)
UVa 11292 - Dragon of Loowater(排序贪心)
UVa 11292 - Dragon of Loowater(排序贪心)
74 0
UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide.
1788 0