Sunday, August 2, 2009

Puzzle 46 - Solution

The puzzle that I had posted (print just the max) is actually a toned down version of the much harder - print out the 10 numbers in ascending order (hence the title Just a sort). You might want to give that a try at your own leisure (and remember writing more than 25 lines of code is still a strict no-no!!)

Here is a solution that I picked up from TheMalkolm which covers the essence of how this could be done!

private static int max(int x, int y){
return x > y ? x : y;
}

private static printMax(num0, num1..., num9){
return max(num0,max(num1,max(num2,max(num3,max(num4,max(num5,max(num6,max(num7,max(num8,num9)))))))));

@Nash - You took me way too literally. When I said no other classes - I really meant no other classes to be used to sort out the numbers. Sorry for the confusion - I need to be more specific with the words I use. Using native code was a really novel idea!

No comments:

Post a Comment

Solution for this question?