一、题目链接
http://noi.openjudge.cn/ch0106/02/
二、解题思路
⑴ 对于每个苹果而言,陶陶能否摘到它取决于陶陶的手伸直能够达到的最大高度hand加上板凳高度bench是否不
小于该苹果距离地面的高度e;
⑵ 显然,方法count需要注入三个参数:int[] height、int hand、int bench,分别代表所有苹果距离地面
的高度、陶陶的手伸直能够达到的最大高度、板凳的高度,方法count的返回值为int,也即陶陶能够摘到的苹果
数量。
三、程序代码
import java.util.Scanner;
public class Main {
public int count(int[] height, int hand, int bench) {
int ans = 0;
for (int e : height) {
ans = (hand + bench >= e ? ans + 1 : ans);
}
return ans;
}
public static void main(String[] args) {
Main test = new Main();
Scanner input = new Scanner(System.in);
int N = 10;
int[] height = new int[N];
for (int i = 0; i < N; i++) {
height[i] = input.nextInt();
}
int hand = input.nextInt();
System.out.print(test.count(height, hand, 30));
}
}