成績指標
排序成績後,找出最高不及格分數與最低及格分數。
01 / 題目理解
DESCRIPTION
找出班級中的成績指標
及格分數為 60 分。請將所有成績由小到大排序,再找出不及格學生中的最高分,以及及格學生中的最低分。
INPUT
第一行輸入學生人數,第二行輸入每位學生的成績,分數介於 0 到 100。
OUTPUT
輸出排序後的成績、最高不及格分數,以及最低及格分數。若不存在對應分數,分別輸出 best case 或 worst case。
02 / 解題思路
01
排序
使用 sort 將成績由小到大排列,方便分界與輸出。
02
尋找及格分界
lower_bound 找到第一個大於或等於 60 的位置;它的前一格就是最高不及格分數。
03
處理兩種極端情況
分界位置在開頭代表全數及格,輸出 best case;分界位置在結尾代表全數不及格,輸出 worst case。
03 / C++ 實作
SOLUTION / B964.CPPSTL
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int student;
cin >> student;
vector<int> scores(student);
for (int i = 0; i < student; i++) {
cin >> scores[i];
}
sort(scores.begin(), scores.end());
for (int i = 0; i < student; i++) {
if (i > 0) {
cout << ' ';
}
cout << scores[i];
}
cout << '\n';
auto firstPass = lower_bound(scores.begin(), scores.end(), 60);
if (firstPass == scores.begin()) {
cout << "best case\n";
} else {
cout << (firstPass - 1) << '\n';
}
if (firstPass == scores.end()) {
cout << "worst case\n";
} else {
cout << firstPass << '\n';
}
}