The Largest/Smallest Box..

The Largest/Smallest Box..

时间: 1ms        内存:64M

描述:

The following figure shows a rectangular card of width W, length L, and thickness 0. Four x x x squares are cut from the four corners of the card shown by the dotted lines. The card is then folded along the dashed lines to make a box without a cover.

Given the width and height of the box, find the values of x for which the box has maximum and minimum volume.

输入:

The input file contains several lines of input. Each line contains two positive floating point numbers L ( 0 < L < 10, 000) and W ( 0 < W < 10, 000), which indicate the length and width of the card, respectively.

输出:

For each line of input, give one line of output containing two or more floating point numbers separated by a single space. Each floating point number should contain three digits after the decimal point. The first number indicates the value which maximizes the volume of the box, while the subsequent values (sorted in ascending order) indicate the cut values which minimize the volume of the box.

示例输入:

1 1
2 2
3 3

示例输出:

0.167 0.000 0.500
0.333 0.000 1.000
0.500 0.000 1.500

提示:

参考答案(内存最优[760]):

#include <stdio.h>
#include <math.h>
double eps=1e-7;
int main(){
	double ll,ww;
	while(scanf("%lf%lf",&ll,&ww)==2){
		double a=12;
		double b=-4*(ll+ww);
		double c=ll*ww;
		double x=(-b-sqrt(b*b-4*a*c))/(2*a),x2=ll>ww?ww/2:ll/2;
		printf("%.3lf %.3lf %.3lf\n",x,0.0,x2+eps);
	}
	return 0;
}

参考答案(时间最优[0]):

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
    double x1,x2;
    double l,w;
    cout<<setiosflags(ios::fixed)<<setprecision(3);
    while(cin>>l>>w)
    {
        x1=sqrt(((l+w)*(l+w)/3-w*l)/12)+(l+w)/6;
        x2=-sqrt(((l+w)*(l+w)/3-w*l)/12)+(l+w)/6;
        cout<<x2<<" 0.000 "<<x1<<endl;
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注