Baekjoon Algorithm | Problem 1546: Average
Source
https://www.acmicpc.net/problem/1546
Problem
Solve Baekjoon Online Judge problem 1546, Average.
Input
Follow the input format and constraints given in the original problem statement.
Output
Print the answer required by the problem.
Sample Input 1
3 40 80 60
Sample Output 1
75.0
Sample Input 2
3 10 20 30
Sample Output 2
66.666667
Sample Input 3
4 1 100 100 100
Sample Output 3
75.25
Sample Input 4
5 1 2 4 8 16
Sample Output 4
38.75
Sample Input 5
2 3 10
Sample Output 5
65.0
Sample Input 6
4 10 20 0 100
Sample Output 6
32.5
Sample Input 7
1 50
Sample Output 7
100.0
Sample Input 8
9 10 20 30 40 50 60 70 80 90
Sample Output 8
55.55555555555556
Algorithm Classification
- Math
- Arithmetic
Solution
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
int n = Integer.parseInt(br.readLine());
StringTokenizer input = new StringTokenizer(br.readLine(), " ");
double max = Integer.MIN_VALUE;
double[] scores = new double[n];
for (int i = 0; i < n; i++) {
double score = Double.parseDouble(input.nextToken());
if (score > max) {
max = score;
}
scores[i] = score;
}
double sum = 0;
for (int i = 0; i < n; i++) {
sum += scores[i] / max * 100;
}
System.out.println(sum/n);
}
}
}