c++三十分求助!!!

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

AutumnWind_ @ 2023-05-07 18:00:23

#include <bits/stdc++.h>
using namespace std;
int main(){
    int l , m , a , b , count = 0;

    cin >> l >> m ;
    int arr[l] = {} ;
    for (int i = 0 ; i < l ; i ++) arr[i] = 0 ;
    for (int i = 0 ; i < m ; i ++){
        cin >> a >> b ;
        for (int j = a ; j <= b ; j ++){
            arr[j] = 1 ;
        }
    }
    for (int i = 0 ; i < l ; i ++){
        if (arr[i] == 0 ){
            count += 1;
        }
    }
    cout << count << endl; 
    return 0;
}

by hjqhs @ 2023-05-07 18:30:12

树是0到l 所以是≤l 而非 <l


by ALexYjy___ @ 2023-05-07 20:40:13

#include<bits/stdc++.h>
using namespace std;
int N[100010];
int main()
{
    int L,m,sum=0,a,b;
    cin>>L>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>a>>b;
        for(int j=a;j<=b;j++) N[j]=1;
    }
    for(int i=0;i<=L;i++)
    {
            if(N[i]==0)
        {
            sum++;
        }
    }

    cout<<sum;
    return 0;
}

|