Haidde @ 2024-02-05 20:58:42
#include<stdio.h>
int main()
{
int l,m,i,a[10010]={0},j,p;
int u[100]={0},v[100]={0};
scanf("%d %d",&l,&m);
for(i=0;i<m;i++)
{
scanf("%d %d\n",&u[i],&v[i]);
for(j=u[i];j<=v[i];j++)
a[j]=1;
}
for(i=0;i<l;i++)
if(a[i]==0)
p++;
printf("%d",p);
return 0;
}
by _th_tw_on_ @ 2024-02-05 21:11:08
p没初始化,最后一个for循环的条件改成 i<=l
by cyx012113 @ 2024-02-06 10:18:39
我把我的C++代码改成了C的
#include <stdio.h>
int main() {
int a[2], b[10001], l, m, cnt=0;
scanf("%d%d", &l, &m);
for (int i = 0;i <= l;i++) b[i] = 0;
for (int i = 1;i <= m;i++) {
for (int j = 0;j < 2;j++) scanf("%d", &a[j]);
for (int j = 0;j <= l;j++)
if (j >= a[0] && j <= a[1])
b[j] = 1;
}
for (int i = 0;i <= l;i++)
if (b[i] == 0) cnt++;
printf("%d\n", cnt);
return 0;
}
by Haidde @ 2024-02-06 10:40:32
@pgl0321 好的,谢谢大佬