90分求助,第9点出错

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

Hopoyage @ 2023-08-19 17:40:40

#include<iostream>
using namespace std;
int main(){
    int l = 0,m = 0,u = 0,v = 0;
    cin >> l >> m;
    int tree = l + 1;
    int arr[tree];
    for(int i = 0;i < m;i++){
        cin >> u >> v;
        for(int i = u;i <= v;i++){
            arr[i] = 1;
        }
    }
    int count = 0;
    for(int i = 0;i < tree;i++){
        if(arr[i] == 1){
            count++;
        }
    }
    tree -= count;
    cout << tree;
    return 0;
}

by 遥遥领先 @ 2023-08-19 17:58:36

@Hopoyage 是不是应该将for(int i = 0;i < tree;i++){改成for(int i = 1;i <= tree;i++){?没测


by OIerWu_829 @ 2023-08-19 17:59:32

#include <bits/stdc++.h>
using namespace std;
int shu[10000];
void yichu(int u, int v)
{
    for (int i = u; i <= v; i++)
    {
        shu[i] = 0;
    }
}
int main()
{
    int l, m, sum = 0;
    cin >> l >> m;
    for (int i = 0; i <= l; i++)
    {
        shu[i] = 1;
    }
    for (int i = 0; i < m; i++)
    {
        int u, v;
        cin >> u >> v;
        yichu(u, v);
    }
    for (int i = 0; i <= l; i++)
    {
        sum += shu[i];
    }
    cout << sum;
    return 0;
}

by 遥遥领先 @ 2023-08-19 18:00:06

#include <bits/stdc++.h>
using namespace std;
int a[110],b[110];
int sum[10010];
int main()
{
    int n,m;
    cin >> n >> m;
    for (int i = 0;i < m;i++)
    {
        cin >> a[i] >> b[i];
        for (int j = a[i];j <= b[i];j++)
        {
            sum[j] = 1;
        }
    }
    int num = 0;
    for (int i = 0;i <= n;i++)
    {
        if (sum[i] == 0) num++;
    }
    cout << num;
    return 0;
}

这是我的代码


by Hopoyage @ 2023-08-19 18:03:57

@遥遥领先 其实c++我还没学完,你打的这些我看不懂....


by Trump_ @ 2023-08-19 18:04:51

后排


by zhaxunuo @ 2023-08-19 18:07:49

arr数组没有初始化


by zhaxunuo @ 2023-08-19 18:08:55

#include<iostream>
using namespace std;
int main(){
    int l = 0,m = 0,u = 0,v = 0;
    cin >> l >> m;
    int tree = l + 1;
    int arr[tree];
    for(int i=0;i<=tree;i++)arr[i]=0;
    for(int i = 0;i < m;i++){
        cin >> u >> v;
        for(int i = u;i <= v;i++){
            arr[i] = 1;
        }
    }
    int count = 0;
    for(int i = 0;i < tree;i++){
        if(arr[i] == 1){
            count++;
        }
    }
    tree -= count;
    cout << tree;
    return 0;
}

by Hopoyage @ 2023-08-19 18:09:41

@zhaxunuo 原来如此,感谢佬


|