Posted by Ryan on Tuesday, October 27, 2009 at 2:05am.
One approach, simple but with a caveat or two:
You need a double for "smallest absoulte ratio so far". Initialise it to something very big.
As you take in each line,
a) calculate the surface and the volume as two integers
b) calculate the absolute ratio as a double
c) if
the absolute ratio is bigger than the "smallest absolute ratio so far", ignore that line,
else
store your new "smallest absolute ratio so far" and the surface and volume integers
At end, reduce your two integers to their lowest terms by calculating out their prime factors and dividing by the ones they have in common.
Technically, there's a potential gotcha in using a double, since it's possible that two ratios are so close that a double doesn't have enough precision to distinguish between them. However, with dimensions only up to 50, I think you'll be safe.
Afterthought: the input could contain two identical lines, or lines that end up with identical answers, and if I were writing the test, I surely would put such identical lines in! That's OK, because the answer is the same.
OK, my conscience nags me about that double.
To do it right, then instead of taking the lazy way of converting to doubles, and comparing, then:
if your current smallest is, say, 13/6
and your new line is, say, 27/13
you should do the comparison by finding a common denominator. You don't have to waste CPU cycles on finding the lowest; just multiply the denominators:
13 / 6 = 169 / 78
27 / 13 = 162 / 78
and then compare your numerators to test which is smaller.
Related Questions
Biology - When cells increase in size, the surface area to volume ratio ...
bio - How does the surface area-to-volume ratio of a 1-mm cube compare to the ...
volume:ration - A worm has a diameter of 2.4cm and its length is upto 1.9M. a)...
science - 1. For the three cubes shown below, determine their surface area, ...
bio - 3. For the three cubes shown below, determine their surface area, volume ...
Bio - Please Check my work and help me to answer the questions i could not get. ...
Bio - Please Check my work and help me to answer the questions i could not get. ...
geometry-finding ratio - Ratio of the heights of two similar cones is 7/9 what ...
Math - To determine the question use Frogs F and G are 5cm to cube and 40cm to ...
Algebra - To determine the question use Frogs F and G are 5cm to cube and 40cm ...
For Further Reading