samedi 22 septembre 2018

Biggest of Five

I have problem with one simple task:

"Write a program that finds the biggest of 5 numbers that are read from the console, using only 5 if statements:"

Input:

On the first 5 lines you will receive the 5 numbers, each on a separate line

Output:

On the only output line, write the biggest of the 5 numbers

Constraints:

The 5 numbers will always be valid floating-point numbers in the range [-200, 200]

Time limit: 0.1s

Memory limit: 16MB

My solution is:

if (
            firstNum < -200 && firstNum > 200 ||
                    secondNum < -200 && secondNum > 200 ||
                    thirdNum < -200 && thirdNum > 200 ||
                    forthNum < -200 && forthNum > 200 ||
                    fifthNum < -200 && fifthNum > 200) {
        System.out.println("Invalid Number");
    } else {
        double max = Math.max(firstNum, Math.max(secondNum, Math.max(thirdNum, Math.max(forthNum, fifthNum))));
        if (max == (int) max) {
        System.out.println((int) max);
        }
        else {
            System.out.println(max);
        }

    }
}

}

This work correct, but the problems are in time limit:

Test case #1: TLE [>0.100s, 41.15 MB] (0/0)

*Time Limit Exceeded

Regards N.M.

Aucun commentaire:

Enregistrer un commentaire