본문 바로가기
Problem Solving/Baekjoon

[백준] 15577 Prosjek - Data Structure / Java

by graycode 2023. 7. 27.

 문제 링크

 

15577번: Prosjek

Little Ivica received N math grades and wants to calculate their average. He knows that the average of two numbers a and b is calculated as (a + b) / 2, but he still doesn’t know how to do it for multiple numbers. He calculates the average by writing dow

www.acmicpc.net

 

 풀이 과정

 

 풀이 코드

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.PriorityQueue;

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 n = Integer.parseInt(br.readLine());

        PriorityQueue<Double> pq = new PriorityQueue<>();
        while (n-- > 0) pq.offer(Double.parseDouble(br.readLine()));

        double avg = !pq.isEmpty() ? pq.poll() : 0.0;
        while (!pq.isEmpty()) avg = (avg + pq.poll()) / 2.0;

        bw.write(String.valueOf(avg));
        bw.flush();
    }

}

댓글