/** * Run the solution. This method is called from main() */ public void run() { // Create object to read data from standard input this.input = new Scanner(System.in); // Leer la cantidad de valores (N) int cantidad = this.input.nextInt(); // Crear un arreglo de esa cantidad de valores double valores[] = this.leerArreglo(cantidad); double mediana = this.calcularMediana(valores); System.out.printf("%.2f%n", mediana); // Close the standard input this.input.close(); } public double[] leerArreglo(int cantidad) { double valores[] = new double[cantidad]; // Por cada valor for ( int contador = 0; contador < cantidad; ++contador ) { // Leer el valor y guardarlo en el arreglo valores[contador] = this.input.nextDouble(); } return valores; } public double calcularMediana(double valores[]) { // Ordenar los valores en el arreglo java.util.Arrays.sort( valores ); // Si la cantidad de valores es impar if ( valores.length % 2 != 0 ) { // La mediana sera el valor en la posicion N/2 return valores[valores.length / 2]; } // De lo contrario else { // La mediana sera el valor promedio de los valores // en las posiciones N/2-1 y N/2 return (valores[valores.length / 2 - 1] + valores[valores.length / 2]) / 2.0; } } public void imprimirArreglo(double valores[]) { // Imprimir los valores para ver si funciono for ( int contador = 0; contador < Math.min(valores.length, 13); ++contador ) { System.err.printf("valores[%d] = %.2f%n", contador, valores[contador] ); } }