dotcpp1111-- Cylinder(構造最大圓柱體積)

                                                   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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章