Đề bài
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.
Ràng buộc
-1,000,000 ≤ a, b, c ≤ 1,000,000
Ví dụ
Đầu vào:
a = 2
b = 4
c = 9
Kết quả mong đợi:
expected = 5.0
Đầu vào:
long long a = 2;
long long b = 4;
long long c = 9;
Kết quả mong đợi:
double expected = 5.0;
Đầu vào:
long a = 2;
long b = 4;
long c = 9;
Kết quả mong đợi:
double expected = 5.0;
Giải thích
2 + 4 + 9 = 15 and 15 / 3 = 5.
Đầu vào:
a = 1
b = 2
c = 2
Kết quả mong đợi:
expected = 1.666667
Đầu vào:
long long a = 1;
long long b = 2;
long long c = 2;
Kết quả mong đợi:
double expected = 1.666667;
Đầu vào:
long a = 1;
long b = 2;
long c = 2;
Kết quả mong đợi:
double expected = 1.666667;
Giải thích
1 + 2 + 2 = 5 and 5 / 3 ≈ 1.666667. Divide as decimals, not whole numbers.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
Add the three values first, then spread the total across three equal parts.
Gợi ý 2
Use ordinary arithmetic division so non-integer results are preserved.
Gợi ý 3
Watch for languages that do integer division by default, since that would drop the fractional part.
Độ phức tạp mong đợi
O(1) time, O(1) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.