[NOIP2005-普及] 陶陶摘苹果

GitHub跳转原题关系图返回列表

把板凳并入伸手高度,一次线性扫描统计高度不超过「伸手高度+30」的苹果个数,O(10) 判定题。

OJ: roj

题目 ID: 3536

难度:入门

标签:入门模拟python

创建: 2026-10-02 06:05

更新: 2026-10-06 13:27

题目描述

陶陶家院子的苹果树结出 1010 个苹果。已知每个苹果离地高度,以及陶陶伸直手能达到的最大高度。她有一个 3030 厘米高的板凳,踩上去再够。求能摘到的苹果数目(碰到即掉)。

输入:第一行 1010 个整数(苹果高度),第二行 11 个整数(伸手高度)。输出:能摘到的苹果个数。

样例输入:100 200 150 140 129 134 167 198 200 111 / 110,样例输出:5。

思路

踩板凳后最大够到高度为 伸手高度 + 30。遍历 1010 个苹果,统计高度不超过该值的个数即可。

参考代码

Python 版:

python
#!/usr/bin/env python3
# Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
# rbook: -> https://rbook.roj.ac.cn  https://rbook2.roj.ac.cn
# rainboy的学习导航网站: https://idx.roj.ac.cn
# create_at: 2026-10-02 06:05
# update_at: 2026-10-02 06:05

import sys

STOOL = 30  # 板凳高度(厘米):直接够不到时,踩上板凳再伸手


def solve() -> None:
    data = iter(map(int, sys.stdin.buffer.read().split()))

    heights = [next(data) for _ in range(10)]  # 10 个苹果到地面的高度
    reach = next(data) + STOOL                 # 踩上板凳后能达到的最大高度

    print(sum(height <= reach for height in heights))  # 碰到即摘到,数一遍即可


if __name__ == "__main__":
    solve()

C++ 版(同一算法):

cpp
/**
 * Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
 * rbook: -> https://rbook.roj.ac.cn  https://rbook2.roj.ac.cn
 * rainboy的学习导航网站: https://idx.roj.ac.cn
 * create_at: 2026-10-06 13:27
 * update_at: 2026-10-06 13:27
 */
#include <iostream>
using namespace std;

typedef long long ll;

const int STOOL = 30; // 板凳高度 30 厘米

ll a[15]; // 10 个苹果高度

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // 读入 10 个苹果高度
    for (int i = 1; i <= 10; ++i) {
        cin >> a[i];
    }

    ll hand;
    cin >> hand; // 伸手高度

    ll reach = hand + STOOL; // 踩板凳后能碰到的最大高度
    ll cnt = 0; // 能摘到的苹果个数

    for (int i = 1; i <= 10; ++i) {
        if (a[i] <= reach) {
            ++cnt;
        }
    }

    cout << cnt << "\n";
    return 0;
}