首页 > 专题系列 > Java解POJ > POJ 3365 Cylinder [解题报告] Java
2013
11-12

POJ 3365 Cylinder [解题报告] Java

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?

输入:

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

温馨提示:

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.

解题代码:

/* @author: */
//给你一张长为H宽为w的纸,要求做一个体积最大的圆柱,并且使底面圆的半径尽量大!
//做法分两种情况,一种以w为底面圆的周长,一种以H-2R为底面圆的周长,然后取最大值就好了

import java.util.Scanner;
import java.util.Arrays;
public class Main{
  static double pi=Math.PI;
  static double epx=1e-6;
  
 public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
  double w,h,v1,v2,temp,r,d;   
    while(sc.hasNext()){
     w=sc.nextDouble();
     h=sc.nextDouble();
     if(w==0&&h==0) break;
        if(h>w){temp=h;h=w;w=temp;}   
        v1=h*h*(w-h/pi)/(4.0*pi);   
        d=w/(pi+1);   
        if(d>h+epx){/*判断d是否符合条件*/  
           r=h/2.0;   
          v2=pi*r*r*h;   
        }   
        else v2=pi*w*w*h/(4.0*(pi+1)*(pi+1));   
        System.out.printf("%.3f\n",Math.max(v1,v2));   
    }   
   }
  
}

  1. 您没有考虑 树的根节点是负数的情况, 若树的根节点是个很大的负数,那么就要考虑过不过另外一边子树了