2623 - 中央値
2011/09/20
java
pku
poj
2623 -- Sequence Median
Javaです。かんたんな問題。
package p2623;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List<Integer> list = new ArrayList<Integer>();
while (0 < n--) {
int a = scanner.nextInt();
list.add(a);
}
Collections.sort(list);
// System.out.println(list);
double a = 0;
n = list.size();
if (n == 1) {
a = list.get(0);
} else if (n % 2 == 0) {
a += list.get(n / 2);
a += list.get((n / 2) - 1);
a /= 2;
} else {
a = list.get((n / 2));
}
System.out.printf("%.1f", a);
}
}
: