21.05.16 기록

최대 1 분 소요

  • 백준 알고리즘 4344 풀이 완료

    • 내가 푼 4344 풀이 (메모리 15.9MB, 시간 188ms로 통과)
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.StringTokenizer;
    
    public class B4344 {
        public static void main(String[] args) {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            StringBuilder sb = new StringBuilder();
            StringTokenizer st;
    
            try {
                int testCase = Integer.parseInt(br.readLine());
    
                for(int i = 0; i < testCase; i++) {
                    st = new StringTokenizer(br.readLine(), " ");
                    int student =  Integer.parseInt(st.nextToken());
    
                    int[] score = new int[student];
                    int sum = 0;
                    for(int j = 0; j < student; j++) {
                        score[j] = Integer.parseInt(st.nextToken());
                        sum += score[j];
                    }
                    double avg = sum / student;
    
                    int cnt = 0;
                    for(int j = 0; j < student; j++) {
                        if(score[j] > avg) {
                            cnt++;
                        }
                    }
    
                    sb.append(String.format("%.3f", cnt/(double)student*100)).append("%\n");
                }
                System.out.println(sb);
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    


    • 소수점을 반올림하여 출력하는 방법으로는 Math.round()String.format() 그리고 NumberFormat 클래스가 있다.
    • Math.round()의 경우 소수점 첫째 자리가 0이면 이하의 자리는 출력되지 않기 때문에 다음으로 간결한 String.format()을 사용하였다.
    • 위 3가지의 성능테스트 결과로는 Math.round()가 압도적으로 가장 좋다.

카테고리:

업데이트: