(UVA) 10107 - What is the Median? - Solução

import java.io.*;
import java.util.*;
import java.lang.Math ;

class Main {
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main processando = new Main();
        processando.takeInput();
       
        System.exit(0);
    }
   
    void takeInput() throws NumberFormatException, IOException {
        String line = "";
        StringTokenizer tokenizer = null;
       
        int[] vetor = new int[10000];
        int contador = 0;
       
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        while ((line = br.readLine()) != null) {
            tokenizer = new StringTokenizer(line);
            int x = Integer.parseInt(tokenizer.nextToken());

            vetor[contador] = x;
           
            contador++;
           
            Arrays.sort(vetor, 0, contador);
                       
            if (contador%2 == 0) {
                System.out.println((vetor[contador/2]+vetor[(contador/2)-1])/2);
            }
            else {
                System.out.println(vetor[contador/2]);
            }       
        }
       
        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução