問題
Compute the average (arithmetic mean) of three integers. The result can have a fractional part.
Implement average_of_three(a, b, c).
Input
a: The first number. b: The second number. c: The third number.
Output
Return (a + b + c) / 3 as a decimal number.
制約
-1,000,000 ≤ a, b, c ≤ 1,000,000
例
入力:
a = 2
b = 4
c = 9
期待される出力:
expected = 5.0
入力:
long long a = 2;
long long b = 4;
long long c = 9;
期待される出力:
double expected = 5.0;
入力:
long a = 2;
long b = 4;
long c = 9;
期待される出力:
double expected = 5.0;
解説
2 + 4 + 9 = 15 and 15 / 3 = 5.
入力:
a = 1
b = 2
c = 2
期待される出力:
expected = 1.666667
入力:
long long a = 1;
long long b = 2;
long long c = 2;
期待される出力:
double expected = 1.666667;
入力:
long a = 1;
long b = 2;
long c = 2;
期待される出力:
double expected = 1.666667;
解説
1 + 2 + 2 = 5 and 5 / 3 ≈ 1.666667. Divide as decimals, not whole numbers.
解き方のヒント
まずは自分で考えてから、ヒントを1つずつ開いてみましょう。
ヒント 1
Add the three values first, then spread the total across three equal parts.
ヒント 2
Use ordinary arithmetic division so non-integer results are preserved.
ヒント 3
Watch for languages that do integer division by default, since that would drop the fractional part.
目安の計算量
O(1) time, O(1) extra space
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。