100过了,但是求助

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

537wsq @ 2023-05-16 10:08:41

#include <stdio.h>  

int main()
{
    int l, m;
    int n;
    scanf("%d %d",&l, &m);
    int s = l + 1;
    int num[s];
    for (n = 0; n < s; n++)
    {
        num[n] = 1;
    }
    int u,v;
    int i,j;
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < 2; j++)
        {
                scanf("%d %d", &u, &v);
                for (n = u; n <= v; n++)        
                {
                    num[n] = 0;
                }
        }
    }
    int cnt = 0;
    for (n = 0; n < s; n++)
    {
        if (num[n] == 1)
        {
            cnt++;
        }
    }
    printf("%d", cnt);

    return 0;
} 

100分过了,但是自己运行的时候需要输六次数据才运行结束,是哪里出错了吗

500 3
150 300
100 200
470 471
150 300
100 200
470 471
298
--------------------------------
Process exited after 3.775 seconds with return value 0
请按任意键继续. . .

by Terrible @ 2023-05-16 10:26:14

@537wsq 显然,你需要将for (j = 0; j < 2; j++)去掉。

不去掉依然能过的原因:scanf在读入到末尾时,不会修改u,v的值,在你的程序里,u,v不变不会影响结果。


by 537wsq @ 2023-05-16 11:38:19

@Terrible 感谢大佬!明白我问题在哪里了!


|