1 条题解
-
0
【例50.1】陶陶摘苹果
解题思路
先保存10个苹果高度,再读手能达到高度h。借助30厘米板凳,能摘到的条件为高度≤h+30。
正确性说明
按顺序完成所述运算后,每个输入元素都被处理在相应位置或统计项中。保存的中间状态与已处理数据对应,最终输出与题目定义一致。
易错点
等于能碰到也能摘;第二行手高要在读完10苹果后读取。
复杂度
时间O(1),空间O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int a[10]; for (int &x : a) cin >> x; int h; cin >> h; int count = 0; for (int x : a) if (x <= h + 30) count++; cout << count << "\n"; return 0; }
- 1