24h購物| | PChome| 登入
2014-02-17 22:54:00| 人氣1,775| 回應0 | 上一篇 | 下一篇

[UVA][三分] 11232 - Cylinder

推薦 0 收藏 0 轉貼0 訂閱站台

2007/2008 ACM International Collegiate Programming Contest
University of Ulm Local Contest

Problem C: Cylinder

Using a sheet of paper and scissors, you can cut out two faces to form a cylinder in the following way:

  1. Cut the paper horizontally (parallel to the shorter side) to get two rectangular parts.
  2. From the first part, cut out a circle of maximum radius. The circle will form the bottom of the cylinder.
  3. 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?

Input Specification

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.

Output Specification

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.

Sample Input

10 10
10 50
10 30
0 0

Sample Output

54.247
785.398
412.095

In the first case, the optimal cylinder has a radius of about 1.591549, in the second case, the optimal cylinder has a radius of 5, and in the third case, the optimal cylinder has a radius of about 3.621795.

由於有兩種可能的卷法,因此拆成兩種方式討論,而這兩種剛好又是凸性,直接做兩次的三分搜索。

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
const double pi = acos(-1);
double calcVolume(double w, double h, double r) {
    double len = 2 * pi * r;
    double ret = 0;
    if(h - 2*r >= len)
        ret = max(ret, pi*r*r*w);
    //if(w >= len)
    //    ret = max(ret, pi*r*r*(h - 2*r));
    return ret;
}
double calcVolume2(double w, double h, double r) {
    double len = 2 * pi * r;
    double ret = 0;
    if(w >= len)
        ret = max(ret, pi*r*r*(h - 2*r));
    return ret;
}
double ternary_search(double w, double h) {
    double l, r, mid, midmid;//ternary search
    double md, mmd;
    double ret;
    l = 0, r = w/2;
    while(fabs(l-r) > 1e-10) {
        mid = (l+r)/2;
        midmid = (mid+r)/2;
        md = calcVolume(w, h, mid);
        mmd = calcVolume(w, h, midmid);
        if(md >= mmd)
            r = midmid;
        else           
            l = mid;
    }
    ret = max(ret, md);
    l = 0, r = w/2;
    while(fabs(l-r) > 1e-10) {
        mid = (l+r)/2;
        midmid = (mid+r)/2;
        md = calcVolume2(w, h, mid);
        mmd = calcVolume2(w, h, midmid);
        if(md >= mmd)
            r = midmid;
        else           
            l = mid;
    }
    ret = max(ret, md);
    return ret;
}
int main() {
    double h, w;
    while(scanf("%lf %lf", &w, &h) == 2 && w) { // w < h
        printf("%.3lf\n", ternary_search(w, h));
    }
    return 0;
}

台長: Morris
人氣(1,775) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA] 11140 - Little Ali's Little Brother
此分類上一篇:[UVA][BIT] 12002 - Happy Birthday

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文