cylinder.java
来自「Ulm大学2007年竞赛题和解题报告」· Java 代码 · 共 38 行
JAVA
38 行
// Author: Adrian Kuegelimport java.io.*;import java.util.*;import java.text.*;public class cylinder { static double get_volume(double r, double h) { return r*r*Math.PI*h; } public static void main(String [] args) throws Exception { Scanner in = new Scanner(new File("cylinder.in")); DecimalFormat df = new DecimalFormat("0.000",new DecimalFormatSymbols(Locale.US)); while(true) { int w = in.nextInt(); int h = in.nextInt(); if (w == 0 && h == 0) break; // first case: use w as height of cylinder // in this case, we want to maximize the radius of the circle // r <= w/2 && 2*r*PI <= h-2*r -> r <= min(w/2, h/(2*PI+2)) double r1 = Math.min(w/2.0, h/(Math.PI*2+2)); double res = get_volume(r1, w); // second case: // use h-2*r as height of cylinder // r <= w/2 && 2*r*PI <= w -> r <= w / (2*PI) double r2 = w / Math.PI / 2; // maximize r*r*PI*(h-2*r), subject to 0 <= r <= r2 // 2*r*PI*h - 6*r*r*PI = 0 // 2*h = 6*r -> r = h/3 // since we know w <= h, r2 <= h / (2*PI) <= h / 3 // it follows the maximum occurs at r2 res = Math.max(res, get_volume(r2, h-2*r2)); System.out.println(df.format(res)); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?