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.
输出
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
题目大意是给你一张已知长和宽的矩形的纸,让你把他减成两部分,其中一部分剪成圆形做底(只有一个底),另一部分卷成筒做侧面,问 如何剪裁可以使得圆柱的体积最大,求出最大体积
一开始认为是一个类似二分的题目,需要枚举一下底和高。但是无从下手,结果划拉划拉发现好像是一个简单的高数题,嘻嘻!
就是这样的一个图形,剪裁过后我们可以选择用w或者h-2*r当高
w当高时应该同时满足:2*r<=w && h-2*r>=2*pi*r,并且 V= pi*r*r*w 是一个关于 r 的递增函数,求得满足条件的并且可以使得体积最大的 r=min(w/2,h/(2*pi+2)) 然后求出体积 v1=pi*r*r*w ,即为w当高时的最大体积
h-2*r 当高时,应该满足的条件为 2*pi*r <=w ,但是 V = pi*r*r*(h-2*r),求导可以知道 r = =h/3 时取得最大值,所以满足条件并且要使得体积最大的 r=min(w/(2*pi),h/3) 然后求出体积 v2=pi*r*r*(h-2*r),即为 h-2*r 当高时的最大体积
最终结果就是两种情况的最大值喽
#include<cstdio>
#include<string>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
inline int _read() {
char ch = getchar();
int sum = 0;
while (!(ch >= '0' && ch <= '9'))ch = getchar();
while (ch >= '0' && ch <= '9')sum = sum * 10 + ch - 48, ch = getchar();
return sum;
}
const int inf=0x3f3f3f3f;
const int mm=0;
const double pi=acos(-1);
double w,h;
double r;
double v1,v2;
int main()
{
while(scanf("%lf%lf",&w,&h)!=EOF){
if(!w&&!h)
break;
//w 当高
r=min(w/2,h/(2*pi+2));
v1=pi*r*r*w;
//w 当底
r=min(w/(2*pi),h/3);
v2=pi*r*r*(h-2*r);
double res=max(v1,v2);
printf("%.3lf\n",res);
}
return 0;
}