0分求助,大佬们,编译器通过,萌新求助,感谢大佬!!!!

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

ZJdog @ 2024-02-28 12:56:01

#include<stdio.h>

int main() {
    int l, m;
    scanf("%d %d", &l, &m);
    int a[100000];
    for (int i = 0; i < l; i++) {
        a[i] = 1;
        //1表示植树
    }
    int b[10000][2];
    for (int j = 0; j < m; j++) {
        scanf("%d %d", &b[j][0], &b[j][1]);
    }
    for (int k = 0; k < m; k++) {
        for (int n = b[k][0]; n < b[k][1]; n++) {
            a[n] = 0;
        }
    }
    int sum = 0;
    for (int o = 0; o < l; o++) {
        sum += a[o];
    }
    printf("%d", sum - 1);
    return 0;
}

by anke2017 @ 2024-02-28 13:05:08

1.

for (int n = b[k][0]; n < b[k][1]; n++)

应为

for (int n = b[k][0]; n <= b[k][1]; n++)

2.不应该把sum-1.

3.是从0到l,而不是从0到l-1!

验证码r8r8祭


by anke2017 @ 2024-02-28 13:05:56

@ZJdog

验证码wbdj祭


by zhang0430 @ 2024-03-06 19:35:41

#include<bits/stdc++.h>

using namespace std;
int main(){
    int l,m;
    cin >> l >> m;

    int shen = l + 1;
    int tr[shen] = {0};

    while(m--){
        int u,v;
        cin >> u >> v;

        for(int i = u;i <= v;i++){
            if(tr[i] == 0){
                tr[i] = 1;

                shen -= 1;
            }
        }
    }
    cout << shen;

    return 0;
}

|