Advertisement
ehzcl

algoritmos - Lista4 - questao A

Mar 26th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.lang.Math;
  3.  
  4. /*
  5.  * To change this license header, choose License Headers in Project Properties.
  6.  * To change this template file, choose Tools | Templates
  7.  * and open the template in the editor.
  8.  */
  9.  
  10. /**
  11.  *
  12.  */
  13. public class Main {
  14.  
  15.     public static int[] quicksort(int[] array, int inicio, int fim) {
  16.         if (inicio < fim) {
  17.             int pivot = partition(array, inicio, fim);
  18.             quicksort(array, inicio, pivot - 1);
  19.             quicksort(array, pivot + 1, fim);
  20.         }
  21.         return array;
  22.     }
  23.  
  24.     public static int partition(int[] a, int lo, int hi) {
  25.         int pivot = a[hi];
  26.         int i = lo - 1;
  27.         for (int j = lo; j <= hi - 1; j++) {
  28.             if (a[j] <= pivot) {
  29.                 i += 1;
  30.                 swap(a, i, j);
  31.             }
  32.         }
  33.         swap(a, i + 1, hi);
  34.         return i + 1;
  35.     }
  36.  
  37.     public static void swap(int[] array, int i, int j) {
  38.         int temp = array[i];
  39.         array[i] = array[j];
  40.         array[j] = temp;
  41.     }
  42.  
  43.     public static void main(String[] args) {
  44.         Scanner input = new Scanner(System.in);
  45.         int test_cases = input.nextInt();
  46.         int[] casas;
  47.         int test_complete = 0;
  48.         int array_size;
  49.         while (test_complete < test_cases) {
  50.             array_size = input.nextInt();
  51.             casas = new int[array_size];
  52.  
  53.             for (int i = 0; i < casas.length; i++) {
  54.                 casas[i] = input.nextInt();
  55.             }
  56.  
  57.             quicksort(casas, 0, casas.length - 1);
  58.             int middle_home = 0;
  59.             if (casas.length % 2 == 0) {
  60.                 middle_home = (casas.length / 2) - 1;
  61.  
  62.             }
  63.             if (casas.length % 2 == 1) {
  64.                 middle_home = (int) Math.floor((casas.length / 2));
  65.             }
  66.             int distance = 0;
  67.             int position = casas.length - 1;
  68.             while (position > middle_home) {
  69.                 distance += (casas[position] - casas[middle_home]);
  70.                 position--;
  71.                 //System.out.println("distance: " + distance);
  72.             }
  73.             position = 0;
  74.             while (position < middle_home) {
  75.                 distance += (casas[middle_home] - casas[position]);
  76.                 position++;
  77.                 //System.out.println("distance:" + distance);
  78.             }
  79.             for (int i = 0; i < casas.length; i++) {
  80.                 //System.out.println(casas[i] + " ");
  81.             }
  82.             System.out.println(distance);
  83.             test_complete++;
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement