#K59812. Find the Median of a List
Find the Median of a List
Find the Median of a List
You are given a list of integers. Your task is to compute the median of the list. The median is defined as follows:
If the list is empty, output None
.
If the list contains an odd number of elements, the median is the middle element after sorting the list in non-decreasing order.
If the list contains an even number of elements, the median is the average of the two middle elements after sorting the list. Mathematically, if the sorted list is \(a_1, a_2, \ldots, a_n\) and \(n\) is even, then the median is defined as \(\frac{a_{\frac{n}{2}} + a_{\frac{n}{2}+1}}{2}\).
Read the input from stdin
and output the median value to stdout
.
inputFormat
The input is read from stdin and consists of two parts:
- The first line contains a single integer n, which represents the number of elements in the list.
- If n is greater than 0, the second line contains n space-separated integers. If n is 0, there is no second line and the list is considered empty.
outputFormat
Output the median of the list to stdout
. If the list is empty, output None
. When the median is obtained by averaging two middle numbers, print it as a floating-point number with one decimal place (e.g., 25.0
).
5
1 2 3 4 5
3
</p>