a203簡單陣列、排序與區間掃描

TOI 2019/03 新手組:班級名單

由 N 遞減到 1,略過三位缺席者。

前往題目練習 →回報教學錯誤時間 O(N),空間 O(N)(Python 集合 O(1))。

這題在學什麼

將三個請假座號存入集合或標記表,方便檢查某座號是否缺席。

從 N 向下走到 1,只有不在請假集合裡的座號才輸出。

每人一行,固定前綴為 No. 加一個空白,總行數應為 N-3。

Step-by-step animation

班級名單・倒序排除缺席

回報動畫錯誤
步驟 1 / 3

建立缺席集合

班級有 7 人,2、5、7 請假。

2
5
7
應輸出人數
4

Reference solution

回報程式錯誤

完整程式

#include <iostream>
#include <vector>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<bool> absent(n + 1);
    for (int i = 0, x; i < 3; i++) {
        cin >> x;
        absent[x] = true;
    }
    for (int x = n; x >= 1; x--)
        if (!absent[x])
            cout << "No. " << x << '\n';
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多