大佬们,这道题写了一晚上了,还是写不出来,帮帮孩子看看哪错了

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

XTTian @ 2023-10-29 23:42:37

#include<iostream>
using namespace std;
int main()
{
    int l = 0, m = 0, a = 0, b = 0, trees = 0, i = 0;
    int arr[10000]{};
    cin >> l >> m;
    for (i = 0; i <= l; i++)
    {
        arr[i] = 0;
    }
    while(m--)
    {
        cin >> a >> b;
        for (i = a; a <= b; a++)
        {
            arr[i] = -1;
        }
    }
    for (i = 0; i <= l; i++)
    {
        if (arr[i] == 0)
            trees++;
    }
    cout << trees;

    return 0;
}

by ImposterAnYu @ 2023-10-29 23:58:22

        for (i = a; a <= b; a++)
        {
            arr[i] = -1;
        }

这里错了,因为 i = a 在进入循环后就不再执行了,所以你的 i 一直都是 a 的值。


by ImposterAnYu @ 2023-10-29 23:58:42

@XTTian


by XTTian @ 2023-10-30 09:02:58

@ImposterAnYu AC了,谢谢大佬


|