C语言 Cylinder
题目描述:
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.
输入由几个测试用例组成。每个测试用例由两个数字 w 和 h (1≤ w ≤ h ≤100)组成,表示纸张的宽度和高度。最后一个测试用例后面跟着一个包含两个零的行。
输出:
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.
对于每个测试用例,打印一行尽可能大的圆柱体。把这个数字四舍五入到小数点后的3位。
样例输入
10 10
10 50
10 30
0 0
样例输出
54.247
785.398
412.095
代码如下:
#include "stdio.h"
#define pi 3.1415926
int main(){
int i,num=0;
float d,a,r,r1,m,w,h,v,v1,s[100];
while(scanf("%f%f",&w,&h)){
if(w==0&&h==0)
break;
r=h/(2*(pi+1));
if(2*r>w)
r=w/2;
v=pi*r*r*w;
r1=w/(2*pi);
v1=pi*r1*r1*(h-(2*r1));
if(v>v1){
s[num]=v;
}
else
s[num]=v1;
num++;
}
for(i=0;i<num;i++)
printf("%.3f\n",s[i]);
return 0;
}

377

被折叠的 条评论
为什么被折叠?



