http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2374
思路:三分枚举。
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <algorithm> 5 #define maxn 1000100 6 using namespace std; 7 const double eps=1e-12; 8 const double pi=acos(-1.0); 9 10 double w,h; 11 12 double ok(double x,int flag) 13 { 14 double max1=0; 15 double k=h; 16 double s=w; 17 double r=(x>s)?s:x; 18 double ss=pi*(r/2)*(r/2); 19 double ll=pi*r; 20 double c=k-x; 21 double r1=(k-x)>s?s:(k-x); 22 if(s>=ll&&flag==1) 23 { 24 double v1=ss*c; 25 max1=max(max1,v1); 26 } 27 if(c>=ll&&flag==2) 28 { 29 double v2=ss*s; 30 max1=max(max1,v2); 31 } 32 double s1=pi*(r1/2)*(r1/2); 33 double l1=pi*r1; 34 if(s>=l1&&flag==1) 35 { 36 double v3=s1*x; 37 max1=max(v3,max1); 38 } 39 if(x>=l1&&flag==2) 40 { 41 double v4=s1*s; 42 max1=max(max1,v4); 43 } 44 return max1; 45 } 46 47 48 int main() 49 { 50 while(scanf("%lf%lf",&w,&h)!=EOF) 51 { 52 if(w==0&&h==0) break; 53 double l=0,r=h; 54 double ans=0,ans1=0; 55 while(r-l>eps) 56 { 57 double mid1=(r+l)/2; 58 double mid2=(l+mid1)/2; 59 if(ok(mid1,1)>=ok(mid2,1)) 60 { 61 ans=mid1; 62 l=mid2; 63 } 64 else 65 r=mid1; 66 } 67 l=0,r=w; 68 while(r-l>eps) 69 { 70 double mid1=(r+l)/2; 71 double mid2=(l+mid1)/2; 72 if(ok(mid1,2)>=ok(mid2,2)) 73 { 74 ans1=mid1; 75 l=mid2; 76 } 77 else 78 r=mid1; 79 } 80 printf("%.3lf\n",max(ok(ans,1),ok(ans1,2))); 81 } 82 return 0; 83 }