Average value - 1#8358

Average value - 1 (Editorial)

Find the smallest and the largest 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 and the number of such students be .

Finally, compute the average weight of the students by dividing by 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 = 0;
while (scanf("%d", &w[n]) == 1) n++;

Find the smallest element and the largest element 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 and the number of students , 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 and the largest element 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 and the number of students , 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 and the largest element in the array.

max_el = max(lst)
min_el = min(lst)

Compute the sum of the weights and the number of students , 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)