题解 | #求小球落地5次后所经历的路程和第5次反弹的高度#
求小球落地5次后所经历的路程和第5次反弹的高度
https://www.nowcoder.com/practice/2f6f9339d151410583459847ecc98446
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int h = in.nextInt(); System.out.println(toDo(h)); System.out.println(toDo2(h)); } public static double toDo (int a){ double b = a; double c = a; for (int i = 1 ;i<5 ; i ++){//b初始化成a了所以循环四次即可 //下落时经历的路程包括下去和上来,所以是b = b + c/2*2 -->b = b + c; b = b + c; c = c/2.0; } return b; } public static double toDo2(int a ){ double b = a; for (int i = 0;i<5;i++){ b=b/2; } return b; } }