marcelofoxes

1013 The Greatest URIJudge

May 13th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. import java.util.Locale;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.         Locale.setDefault(Locale.US);
  8.         Scanner scanner = new Scanner(System.in);
  9.         int valA = scanner.nextInt();
  10.         int valB = scanner.nextInt();
  11.         int valC = scanner.nextInt();
  12.         scanner.close();
  13.        
  14.         String output = String.format("%d eh o maior", maxOf(valA, valB, valC) );
  15.  
  16.         System.out.println(output);
  17.     }
  18.  
  19.     private static int maxOf(int... values) {
  20.         if (values.length == 2) {
  21.             // só temos 2 valores, podemos retornar o maior e quitar
  22.             return maxOfTwo(values[0], values[1]);
  23.         }
  24.  
  25.         // [1] será o maior de [0] e [1]
  26.         values[1] = maxOfTwo(values[0], values[1]);
  27.  
  28.         // queremos uma nova array sem o [0]
  29.         int[] newValues = new int[values.length - 1];
  30.         System.arraycopy(values, 1, newValues, 0, newValues.length);
  31.  
  32.         return maxOf(newValues);
  33.     }
  34.  
  35.     private static int maxOfTwo(int a, int b) {
  36.         return (a + b + absOf(a - b)) / 2;
  37.     }
  38.  
  39.     private static int absOf(int val) {
  40.         return (val < 0) ? -val : val;
  41.     }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment