본문 바로가기
알고리즘 문제풀이/백준

[알고리즘/백준] 1546 평균(자바)

by jeonghaemin 2021. 9. 18.
728x90

문제

https://www.acmicpc.net/problem/1546

풀이 코드

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());
        int max = Integer.MIN_VALUE; //최고 점수
        int[] scores = new int[n]; //점수 저장

        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++) {
            scores[i] = Integer.parseInt(st.nextToken());
            max = Math.max(max, scores[i]);
        }

        double sum = 0;
        for (int score : scores) {
            //score, max 모두 정수형 int이기 때문에 실수형 double로 타입 캐스팅
            sum += (double)score/max*100;
        }

        System.out.println(sum/n);
    }
}

댓글