#9错 90分

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

siyutan12 @ 2024-02-18 13:45:39


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int l,m;
    int a[11000],sum;
    int q,b;
    int x=0,y;
    cin>>l>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>q>>b;
        for(int j=q;j<=b;j++)
        {
            a[j]=1;
        }
    }
    for(int i=0;i<=l;i++)
    {
        if(a[i]==1)
        {
            x++;
        }
    }
    y=l+1;
    sum=y-x;
    cout<<sum;
    return 0;
}

by __Rickysun__ @ 2024-02-18 14:03:24

@siyutan12 把你的数组的定义移到全局,不然有可能垃圾值为1,影响结果,AC code:

#include<bits/stdc++.h>
using namespace std;
int l,m;
int a[11000],sum;
int q,b;
int x=0,y;
int main()
{
    cin>>l>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>q>>b;
        for(int j=q;j<=b;j++)
        {
            a[j]=1;
        }
    }
    for(int i=0;i<=l;i++)
    {
        if(a[i]==1)
        {
            x++;
        }
    }
    y=l+1;
    sum=y-x;
    cout<<sum;
    return 0;
}

找错误不易,给个关注吧


by gaojizhe05 @ 2024-02-18 14:10:13

@siyutan12 局部变量必须初始化,否则会是随机随机数,不一定为0


by Vincent615 @ 2024-02-18 14:14:44

@gaojizhe05


by __Rickysun__ @ 2024-02-18 14:17:46

@gaojizhe05 不是随机数,是系统上一个使用这个内存的垃圾值


by siyutan12 @ 2024-02-18 16:04:46

DANKE!


|