hdu 2289 cup(二分搜索)

注意:当杯子为圆柱时也要二分,不能直接算。

View Code
 1 /*
 2 Author:Zhaofa Fang
 3 Lang:C++
 4 */
 5 #include <cstdio>
 6 #include <cstdlib>
 7 #include <sstream>
 8 #include <iostream>
 9 #include <cmath>
10 #include <cstring>
11 #include <algorithm>
12 #include <string>
13 #include <utility>
14 #include <vector>
15 #include <queue>
16 #include <stack>
17 #include <map>
18 #include <set>
19 using namespace std;
20 
21 typedef long long ll;
22 #define DEBUG(x) cout<< #x << ':' << x << endl
23 #define REP(i,n) for(int i=0;i < (n);i++)
24 #define REPD(i,n) for(int i=(n-1);i >= 0;i--)
25 #define FOR(i,s,t) for(int i = (s);i <= (t);i++)
26 #define FORD(i,s,t) for(int i = (s);i >= (t);i--)
27 #define PII pair<int,int>
28 #define PB push_back
29 #define MP make_pair
30 #define ft first
31 #define sd second
32 #define lowbit(x) (x&(-x))
33 #define INF (1<<30)
34 
35 
36 
37 const double PI = acos(-1);
38 double volume(double r,double R,double h)
39 {
40     return PI*h*(R*R+R*r+r*r)/3;
41 }
42 
43 int main()
44 {
45     //freopen("in","r",stdin);
46     //freopen("out","w",stdout);
47     int T;
48     scanf("%d",&T);
49     while(T--)
50     {
51         double r,R,H,V;
52         scanf("%lf%lf%lf%lf",&r,&R,&H,&V);
53         double h = r*H/(R-r);
54         if(r == R)h = 0;
55         double le=0,ri=H,hx;
56         while(fabs(ri-le)>1e-8)
57         {
58             hx = (le+ri)/2.0;
59             double rx = r*(h+hx)/h;
60             if(h == 0)rx = r;
61             if(volume(r,rx,hx)<V)le = hx+1e-8;
62             else ri = hx-1e-8;
63         }
64         printf("%.6f\n",hx);
65     }
66     return 0;
67 }
原文地址:https://www.cnblogs.com/fzf123/p/2919377.html