Task. Were given three real numbers [latex]x[/latex], [latex]y[/latex], [latex]z[/latex]. Find [latex]\min (x, y, z)[/latex].
Tests
Input | Output |
---|---|
7 8 9 | 7 |
8771 1346574 1131 | 1131 |
Solution
With if-statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.*; class Main{ public static void main (String[] args) throws java.lang.Exception { Scanner s = new Scanner(System.in); int x = s.nextInt(); int y = s.nextInt(); int z = s.nextInt(); //solution with IF statement if(x < y && x < z) { System.out.println(x); } else if(y < z && y < x) { System.out.println(y); } else { System.out.println(z); } s.close(); } } |
Code refactoring
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.*; class Main{ public static void main (String[] args) throws java.lang.Exception { Scanner s = new Scanner(System.in); int x = s.nextInt(); int y = s.nextInt(); int z = s.nextInt(); //solution with Java Library method System.out.println(Math.min(Math.min(x,y),z)); //solution with code refactoring without IF statement System.out.println(x < y && x < z ? x : y < z && z < x ? y : z); s.close(); } } |