top1编程
← 返回题目
题解

【入门】语文成绩

1 条题解

  • 0
    @ 2026-7-30 1:34:17

    解题思路

    求总分和平均分,再把成绩从高到低排序输出。

    参考代码

    // 先读入题目给出的数据。
    // 再按照题目要求进行计算。
    // 最后按规定格式输出答案。
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main() {
        int n, a[150], sum = 0; cin >> n;
        // 读入成绩并求总分。
        for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; }
        // 从大到小排序,方便输出排名。
        for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (a[i] < a[j]) { int t = a[i]; a[i] = a[j]; a[j] = t; }
        cout << sum << endl << fixed << setprecision(2) << (double)sum / n << endl;
        for (int i = 0; i < n; i++) cout << a[i] << (i + 1 == n ? '\n' : ' ');
        return 0;
    }
    

    复杂度分析

    排序需要 O(n^2) 时间;其余循环按照实际遍历次数计算。代码使用固定大小数组,额外空间复杂度为 O(1) 或 O(n)。

    • 1