#34220: C++ STL應用


110630@chsh.chc.edu.tw (Mingyee)

學校 : 國立彰化高級中學
編號 : 205898
來源 : [1.200.145.48]
最後登入時間 :
2024-01-08 12:57:29
b964. 1. 成績指標 -- 2016年3月apcs | From: [122.118.151.115] | 發表日期 : 2023-03-06 20:21

思路:利用lower_bound 搜尋60及格最低分,upper_bound + 反指標搜尋不及格最高分
實作:

#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);//cin加速
    cout.tie(0);
    int n,input;
    cin >> n;
    vector <int> v;
    while(n--){
        cin>>input;
        v.pb(input);
    }
    sort(all(v));//排序
    for(int x:v){
        cout << x << " ";
    }
    cout << endl;
    auto pass_itr = lower_bound(all(v), 60);//查找60以上(含)最小值
    auto fail_itr = upper_bound(rall(v), 60, greater<int>());//查找60以下最大值
    cout << (fail_itr == v.rend() ? "best case" : to_string(*fail_itr)) << endl << (pass_itr == v.end() ? "worst case" : to_string(*pass_itr));//輸出結果
    return 0;
}

 
ZeroJudge Forum