輸入:多門課程的學生成績(人數、各科分數)。
輸出:四大統計指標。
struct Student {
int id;
vector<float> scores; // 每門課分數
};
struct CourseStat {
string name;
int fail_count = 0;
float fail_rate = 0.0f;
float average = 0.0f;
vector<pair<int, float>> sorted; // (id, score) 降序
array<int, 5> distribution = {0}; // 5 個區間計數
array<float, 5> dist_rate = {0};
};
N、課程數 M。vector<Student> students(N),讀入每生 M 科分數。j = 0..M-1:
(id, score) 至向量。std::sort + lambda)。O(N×M)M 次 O(N log N),總 O(M·N log N)。O(N·M) 存分數 + O(N) 排序暫存。cout << fixed << setprecision(2)。Input:
3 2
1001 85 58
1002 92 76
1003 67 45
Output:
=== Course 1 ===
Average: 81.33
Fail Count: 0 (0.00%)
Sorted: 1002:92 1001:85 1003:67
Distribution:
[0-59]: 0 (0.00%)
[60-69]: 1 (33.33%)
[70-79]: 0 (0.00%)
[80-89]: 1 (33.33%)
[90-100]: 1 (33.33%)
=== Course 2 ===
Average: 59.67
Fail Count: 1 (33.33%)
Sorted: 1002:76 1001:58 1003:45
Distribution:
[0-59]: 2 (66.67%)
[60-69]: 0 (0.00%)
[70-79]: 1 (33.33%)
[80-89]: 0 (0.00%)
[90-100]: 0 (0.00%)
結語: 這是典型的「資料處理 + 統計 + 排序」綜合練習。用 C++ STL(vector, sort, iomanip)可在 100 行內完成核心邏輯,重點在於資料結構正確、邊界處理嚴謹、輸出格式精確。