比样例的输出少一

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

Freya_ @ 2023-12-19 17:09:31


#include<bits/stdc++.h> 
using namespace std;

int N=10005;
int main(){
    int l,m,u,v,total,a[N];
    scanf("%d %d",&l,&m);
    for(int i=1;i<=m;i++){
        scanf("%d %d",&u,&v);
        for(int x=u;x<=v;x++){
            if(a[x]==0) a[x]=1;
        }
    }
    for(int y=1;y<=l;y++){
        if(a[y]==0) total++;
    }
    printf("%d",total);
    return 0;
}

by NO_OI_NO_LIFE @ 2023-12-19 17:12:57

for(int y=0;y<=l;y++){//从0开始(审题
        if(a[y]==0) total++;
    }

by Ahws_rwhy @ 2023-12-19 17:14:47

@Freya_

#include<bits/stdc++.h> 
using namespace std;

int N=100005;
int main(){
    int l,m,u,v,total = 0,a[N];
    scanf("%d %d",&l,&m);
    for(int i=1;i<=m;i++){
        scanf("%d %d",&u,&v);
        for(int x=u;x<=v;x++){
            if(a[x] ==0) a[x]=1;
        }
    }
    for(int y=0;y<=l;y++){
        if(a[y]==0) total++;
    }
    printf("%d",total);
    return 0;
}//测试别人代码

by NO_OI_NO_LIFE @ 2023-12-19 17:21:33

@rwhy @Freya_

#include<bits/stdc++.h> 
using namespace std;

const int N=10005;int l,m,u,v,total,a[N];//const
int main(){

    scanf("%d %d",&l,&m);
    for(int i=1;i<=m;i++){
        scanf("%d %d",&u,&v);
        for(int x=u;x<=v;x++){
            a[x]=1;
        }
    }
    for(int y=0;y<=l;y++){//
        if(a[y]==0) total++;
    }
    printf("%d",total);
    return 0;
}

by Ahws_rwhy @ 2023-12-19 17:25:28

@zyh0516_lucky 数组好像开小了。。。


by NO_OI_NO_LIFE @ 2023-12-19 17:30:29

@rwhy 哪里开小了?


by Ahws_rwhy @ 2023-12-19 17:31:01

@zyh0516_lucky 不知道(开大点就过了。。。)


by NO_OI_NO_LIFE @ 2023-12-19 17:36:49

@rwhy 我懂了。楼主数组开到主函数main里了,加上没有const,所以10005的范围缩了水。我正确码字后也AC了,当然你开大数组也行,不过开到什么范围和为什么开大你是随缘的qwq

#include<bits/stdc++.h> 
using namespace std;

const int N=10005;
int l,m,u,v,total,a[N];
int main(){

    scanf("%d %d",&l,&m);
    for(int i=1;i<=m;i++){
        scanf("%d %d",&u,&v);
        for(int x=u;x<=v;x++){
            if(a[x]==0) a[x]=1;
        }
    }
    for(int y=0;y<=l;y++){
        if(a[y]==0) total++;
    }
    printf("%d",total);
    return 0;
}

by NO_OI_NO_LIFE @ 2023-12-19 17:38:08

@rwhy 楼主最主要的问题就是for循环应该从0开始


by Freya_ @ 2023-12-20 15:40:27

@zyh0516_lucky @rwhy 修改后过了,非常感谢!


|