728x90
반응형
이번 문제는 SOLVED.AC에서 문제를 절사평균으로 구하는 방식입니다.
1. 먼저 N값을 입력을 받습니다.
2. N이 0인 경우 프로그램을 종료합니다. (System.exit(0);)
3. 아니라면 값을 입력받고 정렬합니다. (Array.sort(score));
4. N에 0.15을 곱한 후 이를 반올림합니다.
5. 이를 반복문에 시작과 끝에 적용하여 위로 15%, 아래로 15%을 제외합니다.
6. 나머지 인원의 평균을 구합니다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
try {
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(sc.readLine());
int result = 0;
if (N == 0) {
System.out.println(result);
System.exit(0);
}
int[] score = new int[N];
for (int i = 0; i < N; i++) {
score[i] = Integer.parseInt(sc.readLine());
}
Arrays.sort(score);
BigDecimal a = new BigDecimal(N);
BigDecimal percentage_15 = new BigDecimal("0.15");
BigDecimal value = a.multiply(percentage_15);
value=value.setScale(0, RoundingMode.HALF_UP);
int roundvalue = value.intValue();
BigDecimal sum=new BigDecimal("0");
for(int i=roundvalue;i<N-roundvalue;i++){
sum=sum.add(new BigDecimal(score[i]));
}
if(N - (roundvalue * 2) != 0){
sum=sum.divide(new BigDecimal(N - (roundvalue * 2)), RoundingMode.HALF_UP);
result=sum.intValue();
System.out.println(result);
} else{
System.out.println(0); // If there are no valid votes left after trimming, print out zero.
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
728x90
반응형
'프로그래밍_백준 > Java' 카테고리의 다른 글
Java) 1764 듣보잡 (0) | 2023.09.16 |
---|---|
Java) 10814 나이순 정렬 (2차원 배열 정렬) (0) | 2023.09.16 |
Java) 7568번 덩치 (0) | 2023.09.16 |
Java) 1654번 랜선 자르기 (이진 탐색법) (0) | 2023.09.01 |
Java) 1676번 팩토리얼 0의 개수 (0) | 2023.08.30 |