求助!看了半天不知道问题在哪

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

lww0903 @ 2022-11-07 16:38:11

#include<stdio.h>
int main()
{int l,m,a[10001],i,s[101],j[101],c,b;
scanf("%d%d",&l,&m);
for(i=0;i<m;i++)
scanf("%d%d",&s[i],&j[i]);
for(i=0;i<l+1;i++)
a[i]=500;
for(i=0;i<m;i++)
 {for(c=s[i];c<=j[i];c++)
  a[c]=1;}
 for(i=0;i<l+1;i++)
 if(a[i]==1) b++;
 printf("%d",l+1-b);
 return 0;

}

by hyc12528 @ 2022-11-07 16:42:18

@lww0903 b没初始化


by hyc12528 @ 2022-11-07 16:43:28

@lww0903 你这码风


by lww0903 @ 2022-11-07 16:45:21

@hyc12528 好的好的,老师都不教代码,让我们自己写的,好多人说我代码格式有点不好,请问我咋改进呢


by hyc12528 @ 2022-11-07 16:50:40

@lww0903 我发个参考(修改了),还有问题都说了

#include <stdio.h>
int main() {
    int l, m, a[10001], i, s[101], j[101], c, b = 0;
    scanf("%d%d", &l, &m);
    for (i = 0; i < m; i++)
        scanf("%d%d", &s[i], &j[i]);
    for (i = 0; i < l + 1; i++)
        a[i] = 500;
    for (i = 0; i < m; i++) {
        for (c = s[i]; c <= j[i]; c++)
            a[c] = 1;
    }
    for (i = 0; i < l + 1; i++)
        if (a[i] == 1)
            b++;
    printf("%d", l + 1 - b);
    return 0;
}

by lww0903 @ 2022-11-07 16:52:50

@hyc12528 非常感谢


|