30分求救(你也可以看成70分)

P1047 [NOIP2005 普及组] 校门外的树

zyc_2024 @ 2022-11-19 16:35:50

70分

#include<bits/stdc++.h>
using namespace std;
int main() {
    long long n, m, ans = 0;
    cin >> n >> m;
    bool a[n];
    memset(a, 0, sizeof(a));
    for (int i = 0; i < m; i++) {
        int j, k;
        cin >> j >> k;
        for (int l = j; l <= k; l++) {
            a[l] = 1;
        }
    }
    for (int i = 0; i < n; i++) {
        if (!a[i]) {
            ans++;
        }
    }
    cout << ans + 1;
    return 0;
}

30分

#include<bits/stdc++.h>
using namespace std;
int main() {
    long long n, m, ans = 0;
    cin >> n >> m;
    bool a[n];
    memset(a, 0, sizeof(a));
    for (int i = 0; i < m; i++) {
        int j, k;
        cin >> j >> k;
        for (int l = j; l <= k; l++) {
            a[l] = 1;
        }
    }
    for (int i = 0; i < n; i++) {
        if (!a[i]) {
            ans++;
        }
    }
    cout << ans;
    return 0;
}

我哪错了?


by zyc_2024 @ 2022-11-19 16:36:37

我:怎么水的题都没A


by OoXiao_QioO @ 2022-11-19 16:43:23

@shao28Azyc139

1.数组大小不是 n

2.第二个循环条件是 <=n


by 5k_sync_closer @ 2022-11-19 16:43:31

@shao28Azyc139 建议重新读题捏


by zyc_2024 @ 2022-11-19 21:02:22

。。。。。。


|