Time Limit: 2000MS | Memory Limit: 32768KB | 64bit IO Format: %lld & %llu |
Description
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
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).
Output
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.
Sample Input
3
2 10
3.590 2.719
8.1991 7.189
Sample Output
Case 1: 4.513804324
Case 2: 2.2268848896
Case 3: 33.412886
Source
复习一下数学;
v=(L-2*x)(w-2*x)*x ; 对v求导可得导函数: 12x*x-4(W+L)*x+W*L; 再次求导可得 16*(W*W+L*L-W*L) &&(W>L) 所以一阶导数两个解, 原函数先增后减再增, 即在一阶导数小解处可能取最大值, 右边界竟然不用考虑, 疑惑.
#include <cstdio> #include <cmath> int main() { int T, Q=1; scanf("%d", &T); while(T--) { double L, w, Max; scanf("%lf%lf", &L, &w); double a=12.0 ,
b=(w+L)*-4.0 ,
c=w*L ; Max=L/2.0; if(L>w) Max=b/2.0 ; double in=b*b-4.0*a*c; //double in=16.0*(w*w+L*L-w*L); double deta=sqrt(in); double ans1=(-b+deta)/2.0/a; double ans2=(-b-deta)/2.0/a; double recB=ans2; if(ans1<ans2) recB=ans1; double A=4.0, B=-2*(w+L), C=w*L; double maxRec=(L-2*recB) * (w-2*recB)*recB; printf("Case %d: %.6lf ", Q++, maxRec); /*if((L-2*Max) * (w-2*Max)*Max>maxRec&&Max>recB) // 最右端取值不需要比较一下吗, ! ! maxRec= (L-2*Max) * (w-2*Max)*Max; printf("%.6lf ", maxRec); */ } return 0; }