为什么一棵树也没铲掉啊

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

Aronmy @ 2022-11-26 15:57:47

求助


using namespace std;
int main(){
    int l,m,i1,i2,i0;
    int s=0;
    cin>>l>>m;
    int t[l+1];
    for(i0=0;i0<=l;++i0){
        t[i0]=1;
    }
    int u,v;
    for(i1=0;i1<m;++i1){
        cin>>u>>v;
        for(i2=u-1;i2<v;++i2){
            t[i2]=0;
        }
    }
    for(i0=0;i0<=l;++i0){
        t[i0]=1 ? ++s : 0;
    }
    cout << s;
}

by MysteriousEast @ 2022-11-26 17:29:45

for(i0=0;i0<=l;++i0){
    t[i0]=1 ? ++s : 0;
}

改成

for(i0=0;i0<=l;++i0){
    t[i0]==1 ? ++s : 0;
}

比较用==


by Aronmy @ 2022-12-04 16:39:57

@MysteriousEast 改过了,第一个是wa为什么啊,测试点是保存输入和保存输出


by MysteriousEast @ 2022-12-04 20:28:41

AC code:

#include<iostream>
using namespace std; 
int main(){
    int l,m,i1,i2,i0;
    int s=0;
    cin>>l>>m; 
    int t[l+1]; 
    for(i0=0;i0<=l;++i0){ t[i0]=1; }
    int u,v; 
    for(i1=0;i1<m;++i1){ 
        cin>>u>>v; 
        for(i2=u;i2<=v;++i2){ t[i2]=0; } 
    } 
    for(i0=0;i0<=l;++i0){ 
        t[i0]==1 ? ++s : 0; 
    } 
    cout << s; 
}
for(i2=u-1;i2<v;++i2)

改为

for(i2=u;i2<=v;++i2)

范围问题

AC


by MysteriousEast @ 2022-12-04 20:28:55

@Aronmy


by Aronmy @ 2022-12-10 16:45:42

@MysteriousEast 感谢大佬鸭


by MysteriousEast @ 2022-12-10 17:04:15

不用不用,一起进步


|