lubrious @ 2023-10-24 23:41:27
#include <stdio.h>
int main()
{
int l,m;
scanf("%d %d",&l,&m);
int a[10000]={0};
int tree[1000]={1};
int i;
for(i=0;i<=l;i++){
tree[i]=1;
}
for(i=0;i<m*2;i++){
scanf("%d",&a[i]);
}
int j;
for(i=0;i<m*2;i+=2){
for(j=a[i];j<=a[i+1];j++){
if(tree[j]==1){
tree[j]=0;
}
}
}
int sum = 0;
for(i=0;i<=l;i++){
sum=sum+tree[i];
}
printf("%d",sum);
return 0;
}
by zhang_feng_rui @ 2023-10-25 06:34:05
l最大有一万,tree数组的范围应该是10000才行
另外还有两点:
1.不要把范围刚好设成10000,至少应该设10001,否则很容易越界的;
2.最好把建数组这步放在int main外面,否则有可能会wa然后找半天都不知道自己错哪
AC代码:
#include <stdio.h>
int a[10010];
int tree[10010];
int main()
{
int l,m;
scanf("%d %d",&l,&m);
int i;
for(i=0;i<=l;i++){
tree[i]=1;
}
for(i=0;i<m*2;i++){
scanf("%d",&a[i]);
}
int j;
for(i=0;i<m*2;i+=2){
for(j=a[i];j<=a[i+1];j++){
if(tree[j]==1){
tree[j]=0;
}
}
}
int sum = 0;
for(i=0;i<=l;i++){
sum=sum+tree[i];
}
printf("%d",sum);
return 0;
}
by lubrious @ 2023-10-25 13:07:12
谢谢大佬