### 121 - Pipe Fitters

UVa網站題目連結
My Solved Problems Performance

## Background

Filters, or programs that pass ``processed'' data through in some changed form, are an important class of programs in the UNIX operating system. A pipe is an operating system concept that permits data to ``flow'' between processes (and allows filters to be chained together easily.)

This problem involves maximizing the number of pipes that can be fit into a storage container (but it's a pipe fitting problem, not a bin packing problem).

## The Problem

A company manufactures pipes of uniform diameter. All pipes are stored in rectangular storage containers, but the containers come in several different sizes. Pipes are stored in rows within a container so that there is no space between pipes in any row (there may be some space at the end of a row), i.e., all pipes in a row are tangent, or touch. Within a rectangular cross-section, pipes are stored in either a grid pattern or a skew pattern as shown below: the two left-most cross-sections are in a grid pattern, the two right-most cross-sections are in a skew pattern. Note that although it may not be apparent from the diagram, there is no space between adjacent pipes in any row. The pipes in any row are tangent to (touch) the pipes in the row below (or rest on the bottom of the container). When pipes are packed into a container, there may be ``left-over'' space in which a pipe cannot be packed. Such left-over space is packed with padding so that the pipes cannot settle during shipping.

## The Input

The input is a sequence of cross-section dimensions of storage containers. Each cross-section is given as two real values on one line separated by white space. The dimensions are expressed in units of pipe diameters. All dimensions will be less than . Note that a cross section with dimensions can also be viewed as a cross section with dimensions .

## The Output

For each cross-section in the input, your program should print the maximum number of pipes that can be packed into that cross section. The number of pipes is an integer -- no fractional pipes can be packed. The maximum number is followed by the word ``grid'' if a grid pattern results in the maximal number of pipes or the word ``skew'' if a skew pattern results in the maximal number of pipes. If the pattern doesn't matter, that is the same number of pipes can be packed with either a grid or skew pattern, then the word ``grid'' should be printed.

## Sample Input

`3 32.9 102.9 10.511 11`

## Sample Output

`9 grid29 skew30 skew126 skew`

## Solution

`import java.io.BufferedInputStream;import java.util.Scanner;public class Main {    public static void main (String[] args) {        double double1, double2;        int grid, skew;        Scanner in = new Scanner(new BufferedInputStream(System.in));        while (in.hasNext()) {            double1 = in.nextDouble();            double2 = in.nextDouble();            grid = (int)(double1) * (int)(double2);            skew = skew(double1, double2);            if (grid>=skew) {                System.out.println(grid + " grid");            } else {                System.out.println(skew + " skew");            }        }    }    private static int skew(double double1, double double2) {        if ((double1<1)|(double2<1)) {            return 0;        } else {            int skew1 =(((int)((double1-1)/(1.732/2))+1+1)/2 * (int)(double2) +                        ((int)((double1-1)/(1.732/2))+1)/2 * (int)(double2-0.5));            int skew2 =(((int)((double2-1)/(1.732/2))+1+1)/2 * (int)(double1) +                        ((int)((double2-1)/(1.732/2))+1)/2 * (int)(double1-0.5));            return (skew1>skew2) ? skew1 : skew2;        }    }}`