[LightOJ] [1297] Largest Box

Problem Description

Loading...

Problem Analysis

A fairly simple math problem
Find the maximum possible volume
From the relation we can set up a cubic equation in one variable for the volume
Differentiating, and from the problem statement we know
V’=0 must have a solution
V reaches its maximum at the smaller root

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include<stdio.h>
#include<math.h>
double l,w;
double f(double m) {
	return 4*m*m*m-(2*l+2*w)*m*m+l*w*m;
}
int main() {
	int T,kase=0;
	scanf("%d",&T);
	while(T--) {
		scanf("%lf %lf",&l,&w);
		double t=(4*l+4*w-sqrt((4*l+4*w)*(4*l+4*w)-48*l*w))/24;
		printf("Case %d: %lf\n",++kase,f(t));
	}
	return 0;
}

Problem link: 【Light】[1297]Largest Box

Loading...