In the following figure you can see a rectangular card. The width of the card is W and length of the card is L and thickness is zero. Four (x*x) squares are cut from the four corners of the card shown by the black dotted lines. Then the card is folded along the magenta lines to make a box without a cover.
Given the width and height of the box, you will have to find the maximum volume of the box you can make for any value of x.
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing two real numbers L and W (0 < L, W < 100).
For each case, print the case number and the maximum volume of the box that can be made. Errors less than 10-6 will be ignored.
3
2 10
3.590 2.719
8.1991 7.189
Case 1: 4.513804324
Case 2: 2.2268848896
Case 3: 33.412886
大概有6天没有更过博客了吧。。。弱鸡被打击的有点狠(可怕的ccpc...),最近在学算法学的有点崩溃,还是听师傅的写写思维题吧。。。翻了翻以前的数学专练,看到这道题用了三分,没学过,后来看了博客才知道相当于求二次函数最大值,,,那么就可以在原来二分的基础上再取一个中点,谁离取最大值的点远谁被舍弃,,,,
下面附上代码:
#include<bits/stdc++.h>
#define eps 1e-8
using namespace std;
double n,m;
double MaxS(double x)
{
return x*n*m-2*x*x*(n+m)+4*x*x*x;
}
int main()
{
int t,k=0;
cin>>t;
while(t--)
{
scanf("%lf %lf",&n,&m);
double l=0,r=min(n,m)/2,mid,mm;
while(r-l>eps)
{
mid=(l+r)/2;
mm=(mid+r)/2;
if(MaxS(mid)>=MaxS(mm))
r=mm;
else l=mid;
}
printf("Case %d: %lf\n",++k,MaxS(mid));
}
return 0;
}