백준 - 평균은 넘겠지[java]

스브코·2021년 11월 11일

문제출처: https://www.acmicpc.net/problem/4344

문제 설명

입력

첫째 줄에는 테스트 케이스의 개수 C가 주어진다.

둘째 줄부터 각 테스트 케이스마다 학생의 수 N(1 ≤ N ≤ 1000, N은 정수)이 첫 수로 주어지고, 이어서 N명의 점수가 주어진다. 점수는 0보다 크거나 같고, 100보다 작거나 같은 정수이다.

출력

각 케이스마다 한 줄씩 평균을 넘는 학생들의 비율을 반올림하여 소수점 셋째 자리까지 출력한다.

예제 입력

5
5 50 50 70 80 100
7 100 95 90 80 70 60 50
3 70 90 80
3 70 90 81
9 100 99 98 97 96 95 94 93 91

예제 출력

40.000%
57.143%
33.333%
66.667%
55.556%

문제 풀이

import java.io.*;

public class Main {
    public static void main (String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int total = Integer.parseInt(br.readLine());

        for(int i = 0; i < total; i++) {
            String [] numLine = br.readLine().split(" ");
            int sum = 0;

            for (int j = 1; j < numLine.length; j++)
                sum += Integer.parseInt(numLine[j]);

            double average = sum / ((double) numLine.length - 1);
            int count = 0;

            for (int j = 1; j < numLine.length; j++) {
                if(Integer.parseInt(numLine[j]) > average)
                    count++;
            }
            double overAverage = count / ((double) numLine.length - 1) * 100;
            String av = String.format("%.3f", overAverage);
            bw.write(av + "%\n");
        }
        br.close();
        bw.flush();
        bw.close();
    }
}

double 소수점 3자리 수까지 출력하는 법

	double overAverage = count / ((double) numLine.length - 1) * 100;
        String av = String.format("%.3f", overAverage);

까먹어서 찾아봤다...

profile
익히는 속도가 까먹는 속도를 추월하는 그날까지...

0개의 댓글