题解 | #求小球落地5次后所经历的路程和第5次反弹的高度#
求小球落地5次后所经历的路程和第5次反弹的高度
https://www.nowcoder.com/practice/2f6f9339d151410583459847ecc98446
这里面主要是几个隐藏的坑,第一个坑是,球的总经过距离,我第一次算的是每次弹起来高度想加,第二个坑是,要的第一个结果是第五次落地的经过距离,而第二个结果是第五次弹起来,这个弹起来的距离是不要算在落地距离中的需要减去;
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int height = in.nextInt(); LinkedList<Double> result = reboundHeightFive(height); Double reduce = result.stream().reduce(0.0, (a, b) -> a + b); System.out.println(reduce-result.getLast()); System.out.println(result.getLast()); } } private static LinkedList<Double> reboundHeightFive(int height) { return reboundHeight(height,5); } private static LinkedList<Double> reboundHeight(int height, int count) { LinkedList<Double> result = new LinkedList<>(); double h = height; for (int i = 0; i < count; i++) { result.add(h); h = h/2; result.add(h); } return result; } }