簡単 0件解決済み 正答率0%

Array Average

Compute the average of a list of numbers.

このチャレンジで学べること

  • Sum values and divide by the count
  • Return a decimal result
  • Avoid integer division mistakes

問題

翻訳を表示

Return the arithmetic mean of the list: its sum divided by its length, as a decimal number.

Implement array_average(numbers).

Input

numbers: A non-empty list of integers.

Output

Return the average.

制約

1 ≤ length ≤ 100,000; -10⁹ ≤ each value ≤ 10⁹

例

例 1
入力:
numbers = [2, 4, 6]

期待される出力:
expected = 4.0
入力:
std::vector<long long> numbers = {2, 4, 6};

期待される出力:
double expected = 4.0;
入力:
long[] numbers = new long[] {2, 4, 6};

期待される出力:
double expected = 4.0;

解説

(2 + 4 + 6) / 3 = 4.

例 2
入力:
numbers = [1, 2]

期待される出力:
expected = 1.5
入力:
std::vector<long long> numbers = {1, 2};

期待される出力:
double expected = 1.5;
入力:
long[] numbers = new long[] {1, 2};

期待される出力:
double expected = 1.5;

解説

(1 + 2) / 2 = 1.5; divide as decimals.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

The average comes from combining the whole list into one total and then using its length.

ヒント 2

A single pass can collect the sum, then divide by how many values there are.

ヒント 3

Be careful to preserve decimal precision instead of using integer division.

目安の計算量

O(n) time, O(1) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。