Skating Score
National Olympiad — Municipal round (2013 season)
Submission name: skating.cpp
Problem summary
Six judges give integer scores. If they are all equal, sum all six. Otherwise discard one highest and one lowest score and sum the other four.
Input
Six space-separated integer scores.
Output
The final score.
Constraints
There are six scores; the maximum judge score is 20. The PDF gives no lower bound.
Worked example
An independently prepared example for this explanation.
Input
8 2 3 9 5 6
Output
22
Solution analysis
- Accumulate the sum, minimum and maximum of the six scores. Equality of minimum and maximum means every judge agreed.
- For unanimous scores print the sum unchanged. Otherwise subtract the minimum and maximum once each, even if an extreme occurs several times.
- The sample sum is 33. Removing 2 and 9 leaves 22. If all scores were 8, the special rule would give 48.
Why it works
The extrema coincide exactly when all scores coincide. In the other case subtracting one occurrence of each extreme removes exactly the two required scores, so both branches implement the definition.
Watch out. Do not discard every occurrence of the extrema. The unanimous exception keeps all six scores.
Commented C++17 solution
An independently written complete program with standard headers.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int score;
cin >> score;
int total = score;
int lowest = score;
int highest = score;
for (int i = 1; i < 6; i++) {
cin >> score;
total += score;
lowest = min(lowest, score);
highest = max(highest, score);
}
// Unanimous scores are the exception: discard nothing.
if (lowest != highest) {
total -= lowest + highest;
}
cout << total << '\n';
}