1 条题解

  • 0
    @ 2048-9-27 21:35:34

    练60.1 丢失的数字

    解题思路

    开n+1大小的出现表,读m个数时标记。再枚举1~n,未出现就逐行输出。

    正确性说明

    按顺序完成所述运算后,每个输入元素都被处理在相应位置或统计项中。保存的中间状态与已处理数据对应,最终输出与题目定义一致。

    易错点

    输出的是没出现的数,不是输入数排序。

    复杂度

    时间O(n+m),空间O(n)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n, m;
        cin >> n >> m;
        vector<bool> seen(n + 1);
        while (m--) {
            int x;
            cin >> x;
            seen[x] = true;
        }
        for (int i = 1; i <= n; i++)
            if (!seen[i])
                cout << i << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    391
    时间
    1000ms
    内存
    64MiB
    难度
    进阶
    标签
    递交数
    0
    已通过
    0
    上传者