1 条题解
-
0
练52.1 输出成绩
解题思路
保存成绩并求总和S,均值S/n打印三位小数。筛选时用score*n>S精确判断严格高于均值,按原次序逐行输出。
正确性说明
按顺序完成所述运算后,每个输入元素都被处理在相应位置或统计项中。保存的中间状态与已处理数据对应,最终输出与题目定义一致。
易错点
不要用已四舍五入的均值来比较。
复杂度
时间O(n),空间O(n)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<long long> a(n); long long sum = 0; for (auto &x : a) { cin >> x; sum += x; } cout << fixed << setprecision(3) << double(sum) / n << "\n"; for (long long x : a) if (x * n > sum) cout << x << "\n"; return 0; }
- 1