Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. package magicbox;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class DivideEtImpera {
  6.     private static final Scanner cin = new Scanner(System.in);
  7.     private static int n;
  8.     private static int[] a;
  9.  
  10.     // a = 1, 2, 4, 7, 5, 9, 4
  11.     //     1  2  3  4  5  6  7
  12.  
  13.     private static int getMaxTeo(int left, int right) {
  14.         int maxVal = 0;
  15.         for (int i = left; i <= right; ++i)
  16.             if (maxVal <= a[i]) maxVal = a[i];
  17.         return maxVal;
  18.     }
  19.  
  20.     private static int getMaxDAC(int left, int right) {
  21.         if (left == right)
  22.             return a[left];
  23.         if (left + 1 == right)
  24.             return Math.max(a[left], a[right]);
  25.         int middle = (left + right) / 2;
  26.         return Math.max(getMaxDAC(left, middle), getMaxDAC(middle + 1, right));
  27.     }
  28.  
  29.     public static void main() {
  30.         n = cin.nextInt();
  31.         a = new int[n + 1];
  32.         for (int i = 1; i <= n; ++i)
  33.             a[i] = cin.nextInt();
  34.         System.out.println("The max value calculated with the normal method: " + getMaxTeo(1, n));
  35.         System.out.println("The max value calculated with divide and conquer is: " + getMaxDAC(1, n));
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement