题解 | #求小球落地5次后所经历的路程和第5次反弹的高度#
求小球落地5次后所经历的路程和第5次反弹的高度
https://www.nowcoder.com/practice/2f6f9339d151410583459847ecc98446
递归思想 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { static double first=0.0; static int k=1; static double sum=0.0; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); first = Double.valueOf(br.readLine()); k = 5; double ak=an(k); sum=sum-an(k)*2; System.out.println(sum); System.out.println(ak); } public static double an(int n) { //k是第几次 //f(k)=f(k-1)/2.0 //f(0)=n; if (n == 0) { sum+=first; return first; } else { sum+=an(n - 1); return an(n - 1) / 2.0; } } }