516.Find the Exact Average of Two Integers
Easy
Given two non-negative integers
a and b, return their average as text with exactly two decimal places, for example "3.50".
This problem exists to show type castingType CastingExplicitly converting a value from one type to another, e.g. (double) total turns an int into a double. at work. Dividing one int by another throws away the fractional part, so 7 / 2 quietly becomes 3. Casting one side to a doubledoubleA 64-bit floating-point type that can hold fractional values such as 3.5. first keeps the half. The first solution avoids decimals completely and rebuilds the answer from the whole part and the remainder; the second casts before dividing, which is the usual way to get an exact average.
Example 1:
Input: a = 3, b = 4
Output: "3.50"
Example 2:
Input: a = 10, b = 20
Output: "15.00"
+ 5 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ a, b ≤ 1000000
a =
3
b =
4