HDU 2289 Cup 数学+二分

这道题的题意是:给你一个杯子,然后告诉你杯子的下顶面的半径,上顶面的半径,杯子总的高度,还有就是杯子中水的体积。(这里水的体积竟然可以比杯子还大啊!),然后输出现在杯中水的高度。

注意这里的模型是一个圆台。圆台的面积公式是V = PI*(r1*r1+r1*r2+r2*r2)*h/3。

主要是想到要把圆台补成圆锥,然后用相似三角形找到高度比与半径比的关系,之后可以用数学方法推算出比例,然后求出高度h',也可以用二分枚举高度。注意二分的精度要小于10^(-7)。


Cup

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


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
二分算法:

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898

const int maxn = 250100;
using namespace std;

int main()
{
    int n;
    cin >>n;
    while(n--)
    {
        double r1, r2, h, v;
        cin >>r1>>r2>>h>>v;
        if(PI*(r1*r1+r1*r2+r2*r2)*h/3 < v)
        {
            printf("%.6lf\n",h);
            continue;
        }
        if(r1 == r2)
        {
            printf("%.6lf\n",v/(PI*r1*r1));
            continue;
        }
        double hh = r1*h/(r2-r1);
        double l = 0.0, r = h;
        double mid;
        while(r-l > eps)
        {
            mid = (r+l)/2;
            double h1 = mid+hh;
            double x = mid*(r2-r1)/h;
            double vv = ((r1+x)*(r1+x)*h1-r1*r1*hh)*PI/3.0;
            if(v > vv)
                l = mid;
            if(v < vv)
                r = mid;
        }
        printf("%.6lf\n",mid);
    }
    return 0;
}

数学做法:

#include <algorithm>  
#include <iostream>  
#include <stdlib.h>  
#include <string.h>  
#include <iomanip>  
#include <stdio.h>  
#include <string>  
#include <queue>  
#include <cmath>  
#include <stack>  
#include <map>  
#include <set>  
#define eps 1e-7  
#define M 1000100  
#define LL __int64  
#define INF 0x3f3f3f3f  
#define PI 3.1415926535898  
  
const int maxn = 250100;  
using namespace std;  
  
int main()  
{  
    double r1, r2, h, v;  
    int n;  
    cin >>n;  
    while(n--)  
    {  
        cin >>r1>>r2>>h>>v;  
        double v1 = PI*(r1*r1+r2*r2+r1*r2)*h/3.0;  
        if(v > v1)  
        {  
            printf("%.6lf\n",h);  
            continue;  
        }  
        if(r1 == r2)  
        {  
            double H;  
            H = v/(PI*r1*r1);  
            printf("%.6lf\n",H);  
            continue;  
        }  
        double hh=(r1*h)/(r2-r1);  
        double H = hh+h;  
        double vv = PI*r1*r1*hh/3.0;  
        double V = PI*r2*r2*H/3.0;  
        printf("%.6lf\n",pow((vv+v)/V,1.0/3)*H-hh);  
    }  
    return 0;  
}  


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值