求纠错

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

jnxx_liyanze @ 2023-12-21 23:12:51

#include<iostream>
using namespace std;
int tree[10010]={0};
int main(){
    int l,m,a,b,s=0;
    cin>>l>>m;
    for(int i=0;i<m;i++){
        cin >> a >> b;
        for(int j=a;j<=b;j++)
            tree[j]=1;
    }
    for(int i=0;i<=1;i++)
        if(tree[i]==0)
            s++;
    cout<<s<<endl;
    return (0);
}

by hualuo_2 @ 2023-12-23 17:23:24

第二个for的边界打错了

#include<iostream>
using namespace std;
int tree[10010]={0};
int main(){
    int l,m,a,b,s=0;
    cin>>l>>m;
    for(int i=0;i<m;i++){
        cin >> a >> b;
        for(int j=a;j<=b;j++)
            tree[j]=1;
    }
    for(int i=0;i<=l;i++)
        if(tree[i]==0)
            s++;
    cout<<s<<endl;
    return (0);
}

by hualuo_2 @ 2023-12-23 17:23:58

@jnxx_liyanze


by jnxx_liyanze @ 2023-12-24 21:44:04

@hualuoyan2 谢谢


|