Baekjoonアルゴリズム | 1546番問題: 平均

出典

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

問題

Baekjoon Online Judgeの1546番問題、平均を解きます。

入力

正確な入力形式と制約は元の問題文に従います。

出力

問題で求められる答えを出力します。

サンプル入力 1

3 40 80 60

サンプル出力 1

75.0

サンプル入力 2

3 10 20 30

サンプル出力 2

66.666667

サンプル入力 3

4 1 100 100 100

サンプル出力 3

75.25

サンプル入力 4

5 1 2 4 8 16

サンプル出力 4

38.75

サンプル入力 5

2 3 10

サンプル出力 5

65.0

サンプル入力 6

4 10 20 0 100

サンプル出力 6

32.5

サンプル入力 7

1 50

サンプル出力 7

100.0

サンプル入力 8

9 10 20 30 40 50 60 70 80 90

サンプル出力 8

55.55555555555556

アルゴリズム分類

  • 数学
  • 四則演算

解説

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);
        }
    }
}