1297 - Largest Box
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
题意:给定一个矩形的纸,然后在四个角都剪一个边长为x的正方形,求剪后能做出来的盒子的最大体积
思路:体积公式为:v=x*(L-2*x)*(W-2*x),然后就是求一元三次方程的最大值,直接求导计算就行了。
ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
#include<algorithm>
#define fab(a) (a)>0?(a):(-a)
#define MAXN 100010
#define INF 0xfffffff
using namespace std;
double l,w;
double fun(double x)
{
return x*(l-2*x)*(w-2*x);
}
int main()
{
int t;
int cas=0;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf",&l,&w);
double ps;
ps=(4*l+4*w-(sqrt((4*w+4*l)*(4*w+4*l)-48*l*w)))/24;
printf("Case %d: %.6lf\n",++cas,fun(ps));
}
return 0;
}