题目描述
Using a sheet of paper and scissors, you can cut out two faces to form a cylinder in the following way:
Cut the paper horizontally (parallel to the shorter side) to get two rectangular parts.
From the first part, cut out a circle of maximum radius. The circle will form the bottom of the cylinder.
Roll the second part up in such a way that it has a perimeter of equal length with the circle’s circumference, and attach one end of the roll to the circle. Note that the roll may have some overlapping parts in order to get the required length of the perimeter.
Given the dimensions of the sheet of paper, can you calculate the biggest possible volume of a cylinder which can be constructed using the procedure described above?
输入
The input consists of several test cases. Each test case consists of two numbers w and h (1 ≤ w ≤ h ≤ 100), which indicate the width and height of the sheet of paper.
The last test case is followed by a line containing two zeros.
输出
For each test case, print one line with the biggest possible volume of the cylinder. Round this number to 3 places after the decimal point.
样例输入
10 10
10 50
10 30
0 0
样例输出
54.247
785.398
412.095
题目思路
题目大概:将一张纸剪成两张,剪线与短边平行,做出体积最大的圆柱。
c语言AC代码
#include <stdio.h>
#define pi 3.14159265358
double volume(double w,double h)
{
double temp,r1=0,r2=0,v1=0,v2=0;
//第一种情况,以h-2r为周长,以w为高
r1=h/(2*pi+2);
r1=r1*2>w?w/2:r1;
v1=pi*r1*r1*w;
//第二种情况,以h-2r为高,以w为周长
r2=w/(2*pi);
v2=pi*r2*r2*(h-2*r2);
if(v1>v2)//比较两种情况下哪一种的体积最大
return v1;
else return v2;
}
int main()
{
double w,h,v=0;
while(scanf("%lf%lf", &w, &h) ==2&&w!= 0)
{
v=volume(w,h);
printf("%.3f\n",v);
}
return 0;
}