零分求助,wa和超时

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

wuzixianhaoyoucha @ 2022-11-19 14:38:07


#include <iostream>
using namespace std;

int main() {
    int a[1001];
    int l, m;
    int count = 0;
    cin >> l;
    cin >> m;
    for ( int i = 1; i <= l; i++)
        a[i] = 0;
    for ( int i = 1; i <= m; i++) {
        int c;
        int b;
        cin >> c;
        cin >> b;
        for ( int j = c; j <= b; i++)
            a[i] = 1;
    }
    for ( int i = 1; i <= l; i++) {
        if (a[i] = 0)
            count++;
    }
    cout << count << endl;
    return 0;
}

by wssb1919810 @ 2022-11-19 14:58:20

#include<bits/stdc++.h>
using namespace std;
int a[10001],s[101],e[101],l,m,ans;
int main(){
    cin>>l>>m;
    for(int i=0;i<m;i++){
        cin>>s[i]>>e[i];
        for(int j=s[i];j<=e[i];j++) a[j]=1;
    }
    for(int i=0;i<=l;i++) if(a[i]==0) ans++;
    cout<<ans;
} 

by yanghanchen @ 2022-11-19 15:01:25

#include <iostream>
using namespace std;
int main() 
{
    int l, m;
    int count = 0;
    cin >> l>>m;
    int a[l+1]={0};
    for ( int i = 1; i <= m; i++) {
        int c;
        int b;
        cin >> c>>b;
        for ( int j = c; j <= b; j++)
            a[j] = 1;
    }
    for ( int i = 0; i <= l; i++) {
        if (a[i] == 0)
            count++;
    }
    cout << count << endl;
    return 0;
}

这样改就好啦 @wuzixianhaoyoucha


by wuzixianhaoyoucha @ 2022-11-19 15:04:01

@wssb1919810 感谢大佬!我想问问为什么要加一个s[i]和e[i],像直接用c和b来代替不行吗


by wssb1919810 @ 2022-11-19 15:13:17

@wuzixianhaoyoucha 也可以,个人习惯,大家都不一样的


by wuzixianhaoyoucha @ 2022-11-19 15:13:26

@yanghanchen 感谢感谢,我想问问

for ( int i = 1; i <= l; i++) a[i] = 0;

int a[l+1]={0};

这俩初始化数组有啥区别么


by yanghanchen @ 2022-11-19 15:19:58

for会多计算L次 a[l+1]={0}是说a数组的每一个数为0 数组中会讲


by wuzixianhaoyoucha @ 2022-11-19 15:27:44

@yanghanchen 好的好的


by __mcr130102__ @ 2022-12-24 13:02:26

谢谢给我发答案


|