#1#2#3WA70分求调

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

xingze @ 2024-02-18 16:24:47

测试结果:https://www.luogu.com.cn/record/147331152
代码:

#include <iostream>
using namespace std;
bool l[10050];
long long a,r;
int main()
{
    cin >> a;
    cin >>r;
    for (int i = 0;i<=a;i++)
        l[i]=1;
    while (r--)
    {
        long long x,y;
        cin >> x>>y;
        for (int i = x;i<=y;i++)
            l[i]=0;
    }
    long long ans;
    for (int i = 0;i<a;i++)
    {
        if (l[i])
            ans++;
    }
    cout << ans<<endl;
}

思路与ChargeDonkey的题解相似,求dalao指错!


by hayoon @ 2024-02-18 16:32:29

@xingze

ans没清0


by hayoon @ 2024-02-18 16:32:45

@xingze

long long ans=0;


by xingze @ 2024-02-18 16:34:50

@hayoon 但ans清零后#4,5,6,7,8,9,10都WA了


by hayoon @ 2024-02-18 16:35:43

@xingze
wc6


by wenguangxi1 @ 2024-02-18 16:38:07

l数组的值赋反了

#include <iostream>
using namespace std;
bool l[10050];
long long a,r;
int main()
{
    cin >> a;
    cin >>r;
    for (int i = 0;i<=a;i++)
        l[i]=0;
    while (r--)
    {
        long long x,y;
        cin >> x>>y;
        for (int i = x;i<=y;i++)
            l[i]=1;
    }
    long long ans;
    for (int i = 0;i<a;i++)
    {
        if (l[i])
            ans++;
    }
    cout << ans<<endl;
}

by wenguangxi1 @ 2024-02-18 16:39:06

@xingze


by wenguangxi1 @ 2024-02-18 16:41:47

不是,不要直接复制代码!! 还有还是那个问题ans要赋初始值0


by hayoon @ 2024-02-18 16:44:51

@wenguangxi

l数组不用初始化吧


by wenguangxi1 @ 2024-02-18 16:45:15

@xingze

#include <iostream>
using namespace std;
bool l[10050];
long long a,r;
int main()
{
    cin >> a;
    cin >>r;
    for (int i = 0;i<=a;i++)
        l[i]=0;
    while (r--)
    {
        long long x,y;
        cin >> x>>y;
        for (int i = x;i<=y;i++)
            l[i]=1;
    }
    long long ans=0;
    for (int i = 0;i<a;i++)
    {
        if (l[i])
            ans++;
    }
    cout << ans<<endl;
}

by wenguangxi1 @ 2024-02-18 16:47:24

@hayoon 确实,不过因为是在他的代码上调的所以l数组初始化了


| 下一页