TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

LeanTo

Problem statement, definition, constraints, and public examples.

Problem Statement

             /| 
            / | 
           /  | 
          /   |
         /    | 
        /|    |
       / |    |                  
      /__|____|
A lean-to is a building whose vertical cross-section is a right triangle. Our company is designing a lean-to with one interior wall. The trapezoidal area between the interior and exterior wall is called the "living area" and the remaining triangular area is called the "storage area". We want a very accurate estimate of the height of the interior wall. (The height of the interior wall, the "true height", is a real value that may not be expressible using a finite number of decimal places.)

Your supervisor has suggested an iterative approximation method (see note). He wants your estimate to be the four decimal place value that is closest to the true height. Create a class LeanTo that contains a method height that takes as input an int extHt (the height of the exterior wall), an int livingWidth (the horizontal width of the living area), and an int storageRoof (the length of the roof over the storage area). The method returns the height of the interior wall.

The height should be expressed as a String accurate to 4 decimal places. The String must contain a decimal point, exactly 4 digits to the right of the decimal point, and no leading zeroes. If you find a value that is within .000001 of the true height and round it (.xxxx4x rounds down, .xxxx5x rounds up) to the nearest 4 decimal place result, the second constraint below guarantees that your result will be accurate to 4 decimal places.

                         /|  -|
                        / |   |
    storageRoof        /  |   |
               \      /   |   |
                \/\  /    |   |
                /   /|    |   |-----extHt
               /   / |    |   |              
                \ /__|____|  -|

                     |____|
                       |
                      livingWidth

Definition

Class:
LeanTo
Method:
height
Parameters:
int, int, int
Returns:
String
Method signature:
String height(int extHt, int livingWidth, int storageRoof)
(be sure your method is public)

Notes

  • Hint: do not try to solve for the height algebraically. Use an iterative approximation technique, in which you repeatedly choose a height and check to see if your height is too big or too small to fit into the given lean-to. Keep narrowing the range in which the true height is known to lie.

Constraints

  • extHt, livingWidth, and storageRoof all are between 1 and 30,000 inclusive
  • all values within .000001 of the true height of the interior wall will round to the same 4 decimal place result (where .xxxx4x rounds down, .xxxx5x rounds up)

Examples

  1. 36
    10
    13
    Returns: "12.0000"
    The storage area is a 5 x 12 x 13 right triangle. The base of the entire lean-to is then 15 (5 from the storage area + 10 from the living area) and the whole building is a 15 x 36 x 39 right triangle.
  2. 30000
    1
    30000
    Returns: "29975.3480"
    The true height is 29975.34801....
  3. 1
    30000
    1
    Returns: ".0000"
  4. 1000
    747
    5
    Returns: "4.0000"
    The storage area is a 3 x 4 x 5 right triangle.
  5. 6675
    18091
    18310
    Returns: "3329.5234"
Back to all problems