입력 부분을 보면 n(입력 값)이 0이 될 수도 있다.
입력 값이 0일 경우 절사 평균을 계산할 수 없기 때문에
별도로 예외처리를 해줘야 한다.
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
if (N == 0) { // 입력이 0일 경우 예외 처리
System.out.println(0);
return;
}
int[] scores = new int[N];
for (int i = 0; i < N; i++) {
scores[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(scores);
// 절사할 개수 계산
int trim = (int) Math.round(N * 0.15);
// 중간 값들의 합 계산
int sum = 0;
for (int i = trim; i < N - trim; i++) {
sum += scores[i];
}
// 절사평균 계산 후 반올림
int count = N - 2 * trim;
int avg = (int) Math.round((double) sum / count);
System.out.println(avg);
}
}
'알고리즘 > 백준' 카테고리의 다른 글
Dynamic Programming(DP) (2) | 2024.11.06 |
---|---|
조합 (0) | 2024.11.06 |
[백준 10816번] 숫자 카드 2(Java) : HashMap (0) | 2024.11.03 |
[백준 11724번, 2178번, 1920번] 탐색(Java) (4) | 2024.11.02 |
[백준 1676번] 팩토리얼 0의 개수(Java) (3) | 2024.11.01 |