救救孩子吧

P1046 [NOIP2005 普及组] 陶陶摘苹果

615feifei @ 2022-08-02 22:52:17

#include<iostream>
using namespace std;
int main(){
    int g,p,s;
    cin>>g;
    for(int i=1;i<=10;i++){
        cin>>p;
        if(p<=g+30){
            s++;
        }
    }
    cout<<s;

}

by FQR_ @ 2022-08-02 22:58:33

@615feifei

s没有初始化。

int g,p,s=0;


by FQR_ @ 2022-08-02 23:00:21

还有,读入反了


by FQR_ @ 2022-08-02 23:01:19

先读入每棵树的高度,再读入陶陶的身高


by chanthu2114 @ 2022-08-03 18:39:14

@615feifei,

请楼主复制粘贴抄袭!!!

用得着这么麻烦吗?直接来一波if语句不就搞定了

#include<cstdio>
using namespace std;
int main(){ 
    int aph_1,aph_2,aph_3,
    aph_4,aph_5,aph_6,
    aph_7,aph_8,aph_9,
    aph_10,ans,tao_high;
    scanf("%d%d%d%d%d%d%d%d%d%d\n%d",
    &aph_1,&aph_2,&aph_3,&aph_4,
    &aph_5,&aph_6,&aph_7,&aph_8,
    &aph_9,&aph_10,&tao_high);
    ans=0;
    if(tao_high+30>=aph_1){
        ans++;
    }if(tao_high+30>=aph_2){
        ans++;
    }if(tao_high+30>=aph_3){
        ans++;
    }if(tao_high+30>=aph_4){
        ans++;
    }if(tao_high+30>=aph_5){
        ans++;
    }if(tao_high+30>=aph_6){
        ans++;
    }if(tao_high+30>=aph_7){
        ans++;
    }if(tao_high+30>=aph_8){
        ans++;
    }if(tao_high+30>=aph_9){
        ans++;
    }if(tao_high+30>=aph_10){
        ans++;
    }
    printf("%d",ans);
    return 0;
}

也可以利用真假表达式判断真假,并把真("1")和假("0")相加求得答案,代码如下:

#include<cstdio>
using namespace std;
int main(){
    int aph1,aph2,aph3,
    aph4,aph5,aph6,
    aph7,aph8,aph9,
    aph10,tao_high,ans;
    scanf("%d%d%d%d%d%d%d%d%d%d\n%d",
    &aph1,&aph2,&aph3,&aph4,
    &aph5,&aph6,&aph7,&aph8,
    &aph9,&aph10,&tao_high);
    ans=(tao_high+30>=aph1)+
    (tao_high+30>=aph2)+
    (tao_high+30>=aph3)+
    (tao_high+30>=aph4)+
    (tao_high+30>=aph5)+
    (tao_high+30>=aph6)+
    (tao_high+30>=aph7)+
    (tao_high+30>=aph8)+
    (tao_high+30>=aph9)+
    (tao_high+30>=aph10);
    printf("%d",ans)
    return 0;
}

用你的方法,要对s进行初始化,还要分别读入10棵树的高度,再读入陶陶的身高,你的读入反了。 应该这么写:

#include<iostream>
using namespace std;
int main(){
    int g,p,s=0;
    for(int i=1;i<=10;i++){
        cin>>p;
        if(i==1){
            cin>>g;
        }
        if(p<=g+30){
            s++;
        }
    }
    cout<<s;

}

by gfoi_lyl @ 2022-08-06 16:13:56

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a[10],b,j=0;
    for(int i=0;i<10;i++)cin>>a[i];
    cin>>b;
    for(int i=0;i<10;i++){
       if(a[i]<=b+30)j++;   
    }
    cout<<j;
}

by jushen_x @ 2022-09-03 21:26:44

AC代码(仅供参考):

#include<bits/stdc++.h>

using namespace std;

int k,a[10]; 

int main()

{

int i,count=0;

for(int i=0;i<10;++i)

cin>>a[i];

cin>>k;

for(int i=0;i<10;++i)

if(a[i]<=k+30) ++count;

cout<<count;

return 0;

}

思路:先输入这些数据,直接拿身高加30一个个试,累加起来,得出结果。不客气


by The_DarkForest @ 2022-12-14 14:27:17

@chanthu2114 你这几乎完全是复制粘贴。不过居然AC了,谢谢大佬提供模板。


|