K - Largest Box
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.
代码:
#include <stdio.h>
#include <math.h>
using namespace std;
int t, flag = 0;
double l, w;
int main(){
scanf("%d", &t);
while(t--){
scanf("%lf %lf", &l, &w);
double ans;
ans = (4*l+4*w-(sqrt((4*w+4*l)*(4*w+4*l)-48*l*w)))/24;
ans = ans*(l-2*ans)*(w-2*ans);
printf("Case %d: %.6lf\n", ++flag, ans);
}
return 0;
}