• 문제 링크
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();
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 7585 Brackets - Data Structure / Java (0) | 2023.07.29 |
---|---|
[백준] 26596 황금 칵테일 - Data Structure / Java (0) | 2023.07.28 |
[백준] 14753 MultiMax - Brute Force / Java (0) | 2023.07.26 |
[백준] 9417 최대 GCD - Brute Force / Java (0) | 2023.07.25 |
[백준] 5671 호텔 방 번호 - Brute Force / Java (0) | 2023.07.24 |
댓글