#K49297. Average Speed Calculation
Average Speed Calculation
Average Speed Calculation
You are given a list of non-negative speeds (in km/h). Your task is to compute the average speed rounded to two decimal places. The average is defined by the formula:
\(\text{Average} = \frac{\text{sum of speeds}}{\text{number of speeds}}\)
If the list of speeds is empty, simply output 0
. Otherwise, compute the average and round it as follows:
- If the result is a whole number or has one significant decimal digit (e.g.
30.0
or13.5
), output it with one decimal place. - If it has two non-zero decimal digits, output it with two decimal places.
Note: The input is provided via standard input and the result must be printed to standard output.
inputFormat
The first line contains an integer n
representing the number of speeds. If n > 0
, the second line contains n
space-separated floating point numbers representing the speeds (in km/h). If n
is 0
, there will be no speeds on the next line.
outputFormat
Print the average speed according to the rules described above. If the list is empty, print 0
. Otherwise, print the average speed rounded to one or two decimal places based on the value.
5
20 25 30 35 40
30.0