HDU2289 Cup 二分查找

Cup

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

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

问题连接

问题描述

有个水杯可以看作是一个圆台(也可能是个圆柱),给出杯子的上下半径和高度,以及杯子中水的体积。求水面到杯子底高度。

问题分析

圆台公式:V=π×H×(R×R+r×r+R×r)/3。
容器内的水的形状也是个圆台。
假设水的高度为h,那么由相似三角形可求得水面半径r1。
再由圆台公式可以得到水的体积v,然后与题目给的实际水的体积V比较,如果v>V,那么说明h偏小,如果v<V,说明h偏大,因为容器的形状是固定的,显然v是随h的增大而增大的,当然也可以带入求导看单调性。
所以用二分查找能找到答案(近似值),而且这个办法真的很快。

程序如下

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
const double PI = acos(-1.0);//PI的精度要较高 

int main() {
	int t;
	double r,R,H,V,low,high,mid;
	scanf("%d", &t);
	while (t--) {
		scanf("%lf %lf %lf %lf", &r, &R, &H, &V);
		double cmp=3*V/PI,r1;//Vm=PI*H*(R*R + r * r + R * r) / 3 
		low=0;
		high=R;
		int time=100;
		while(time--){//进行的次数,100次足够了,保证了精度,也不会死循环
			mid=(low+high)/2;
			r1=mid*(R-r)/H+r;//由相似三角形得到h与r1的关系 
			double x=mid*(r*r+r1*r1+r*r1);//将cmp与x作比较 
			if(x>=cmp){//说明找的h偏大 
				high=mid;
			}
			else{
				low=mid;
			}
		}
		printf("%.6lf\n",high);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值