90分求助

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

liuletian @ 2022-09-19 18:42:11

#include<iostream>
using namespace std;
int main()
{
    int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,h,ans;
    cin>>a1>>a2>>a3>>a4>>a5>>a6>>a7>>a8>>a9>>a10>>h;
    if(h>a1)
    {
        ans++;
    }
    if(h>a2)
    {
        ans++;
    }
    if(h>a3)
    {
        ans++;
    }
    if(h>a4)
    {
        ans++;
    }
    if(h>a5)
    {
        ans++;
    }
    if(h>a6)
    {
        ans++;
    }
    if(h>a7)
    {
        ans++;
    }
    if(h>a8)
    {
        ans++;
    }
    if(h>a9)
    {
        ans++;
    }
    if(h>a10)
    {
        ans++;
    }
    if(h<a1&&h+30>=a1)
    {
        ans++;
    }
    if(h<a2&&h+30>=a2)
    {
        ans++;
    }
    if(h<a3&&h+30>=a3)
    {
        ans++;
    }
    if(h<a4&&h+30>=a4)
    {
        ans++;
    }
    if(h<a5&&h+30>=a5)
    {
        ans++;
    }
    if(h<a6&&h+30>=a6)
    {
        ans++;
    }
    if(h<a7&&h+30>=a7)
    {
        ans++;
    }
    if(h<a8&&h+30>=a8)
    {
        ans++;
    }
    if(h<a9&&h+30>=a9)
    {
        ans++;
    }
    if(h<a10&&h+30>=a10)
    {
        ans++;
    }
    else
    {
    }
    cout<<ans;
    return 0;
}

by L_zaa_L @ 2022-09-19 18:46:30

@liuletian 你等于号呢?


by liuletian @ 2022-09-19 18:48:56

@bhbjzyh 谢谢了,我还突然发现ans没定初值


by WA_sir @ 2022-09-19 18:59:55

@liuletian

其实,没必要那么麻烦。

身高+凳子高度>=苹果高度,就说明一定能摘到

一个苹果只要判断一次就可以了

#include<iostream>
using namespace std;
int main()
{
    int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,h,ans=0;
    cin>>a1>>a2>>a3>>a4>>a5>>a6>>a7>>a8>>a9>>a10>>h;
    if(h+30>=a1)
        ans++;
    if(h+30>=a2)
        ans++;
    if(h+30>=a3)
        ans++;
    if(h+30>=a4)
        ans++;
    if(h+30>=a5)
        ans++;
    if(h+30>=a6)
        ans++;
    if(h+30>=a7)
        ans++;
    if(h+30>=a8)
        ans++;
    if(h+30>=a9)
        ans++;
    if(h+30>=a10)
        ans++;
    cout<<ans;
    return 0;
}

by liuletian @ 2022-09-19 19:04:56

@wztony2022 原来还可以这样啊


by tommmy @ 2022-09-23 18:52:30

@liuletian

#include <iostream>
using namespace std;
int main(){
    long long n,x,cnt=0;
    long long numbers[11];
    for(int i=0;i<10;i++){
        cin>>numbers[i];
    }
    cin >> x;
    x += 30;
    for(int i=0;i<10;i++){
        if(x>=numbers[i]){
            cnt++;
        }
    }
    cout<<cnt<<endl;
    return 0;
}

by da_ke @ 2022-09-25 20:54:14

可以用数组


by liuletian @ 2022-09-28 17:38:39

@tommmy 我数组没学好,所以只能这样


|