90 point!Help!(C++14(GCC 9) )

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

Use_Imagination @ 2023-02-11 11:26:53

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,m;
    cin>>n>>m;
    bool a[n+1];
    memset(a,true,sizeof(a));
    for(int i=1;i<=m;i++){
        int x,y;
        cin>>x>>y;
        for(int j=x;j<=y;j++)
            a[j]=false;
    }
    int sum=1;
    for(int i=1;i<=n;i++){
        if(a[i])
            sum++;
    }
    cout<<sum;
    return 0;
}

by Loser_Syx @ 2023-02-11 11:29:34

@luogu_wzh 统计sum的时候i=0


by Use_Imagination @ 2023-02-11 11:32:56

@Saint_ying_xtf 不行,0分


by guozhe6241 @ 2023-02-11 11:34:34

最后循环i=0看看行不行@luogu_wzh


by Use_Imagination @ 2023-02-11 11:35:34

不行,成0分


by Loser_Syx @ 2023-02-11 11:36:20

@luogu_wzh sum初始零


by Loser_Syx @ 2023-02-11 11:36:52

然后i从零开始


by guozhe6241 @ 2023-02-11 11:37:40

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,m;
    cin>>n>>m;
    bool a[n+1];
    memset(a,true,sizeof(a));
    for(int i=1;i<=m;i++){
        int x,y;
        cin>>x>>y;
        for(int j=x;j<=y;j++)
            a[j]=false;
    }
    int sum=0;
    for(int i=0;i<=n;i++){
        if(a[i])
            sum++;
    }
    cout<<sum;
    return 0;
}

这个代码我能A@luogu_wzh


by Use_Imagination @ 2023-02-11 11:37:47

oh,感谢,AC了!


|