The project “Average
Weight of a School Student” was undertaken by Mamed and Samed.
Why they need this
number remains a mystery. They asked all the students in the school to weigh
themselves and recorded the results in a table. Your task is to help them
calculate the average weight of the students. However, there is a condition:
when calculating the average, the students with the highest and lowest weights
should not be included.
It is worth noting
that Mamed and Samed forgot to count the total number of students, but this
will not prevent you from completing the task.
Input. The weights of the
students are given across multiple lines, separated by spaces (one or more) or
by the end-of-line character. The input continues until the end of the file.
Output. Print the average
weight of the students, excluding the ones with the highest and lowest weights.
The result should be rounded to the nearest whole number.
Sample input |
Sample output |
40 23 27 59 68 23 84
27 53 46 |
46 |
array
Algorithm
analysis
Find the
smallest min and the largest max elements in the array. Then, compute the sum of the weights of all students, excluding those
with the smallest or largest values. Let this sum be s and the number of such students be cnt.
Finally, compute the average weight of the students by
dividing s by cnt and print the
result, rounded to the nearest whole kilogram.
Algorithm implementation
Declare the array.
int w[1000];
Read the input data. The count of input numbers is stored in the variable n.
n = 0;
while (scanf("%d", &w[n]) == 1) n++;
Find the
smallest element min and the largest element max in the array.
min = max = w[0];
for (i = 0; i < n; i++)
{
if (w[i] > max)
max = w[i];
if (w[i] < min)
min = w[i];
}
Compute the sum
of weights s and the number of students cnt, excluding those with
the highest and lowest weights.
s = cnt = 0;
for (i = 0; i < n; i++)
if (w[i] != min
&& w[i] != max)
{
s = s + w[i];
cnt++;
}
Print the
result, rounded to the nearest whole kilogram.
printf("%.0lf\n", 1.0 * s / cnt);
Algorithm implementation – min_element, max_element
Declare the array.
vector<int> w;
Read the input data.
while (scanf("%d", &x) == 1)
w.push_back(x);
Find the
smallest element min_el and the
largest element max_el in the
array.
min_el = *min_element(w.begin(), w.end());
max_el = *max_element(w.begin(), w.end());
Compute the
sum of the weights s and the number of students cnt, excluding
those with the highest and lowest weights.
s = cnt = 0;
for (int x : w)
if (x != min_el &&
x != max_el)
{
s += x;
cnt++;
}
Print the
result, rounded to the nearest whole kilogram.
printf("%.0lf\n", 1.0 * s / cnt);
Python implementation
import sys
Read the input data.
lst = []
for line in sys.stdin:
lst.extend(map(int,line.split()))
Find the
smallest element min_el and the
largest element max_el in the list.
max_el = max(lst)
min_el = min(lst)
Compute the
sum of the weights s and the number of students cnt, excluding
those with the highest and lowest weights.
cnt = sum = 0
for x in lst:
if x != max_el and x != min_el:
cnt += 1
sum += x
Print the
result, rounded to the nearest whole kilogram.
res = round(sum / cnt)
print(res)