題目描述
假設一個球從任意高度自由落下,每次落地後反跳回原高度的一半; 再落下, 求它在第5次落地時,共經歷多少米?第5次反彈多高?
最後的誤差判斷是小數點3位
輸入描述:
輸入起始高度,int型
輸出描述:
分別輸出第5次落地時,共經過多少米第5次反彈多高
示例
輸入
1
輸出
2.875
0.03125
題目分析
我剛用自己的 zhu 手,畫了一個圖,就是上邊這張圖了。
這個題,其實可以取巧的:有一種解法是,給案例的數據乘以倍數就行。
2.875 * hight
0.03125 * hight
java 代碼
import java.util.Scanner;
/**
* Created by Feng on 2020/2/21 15:36
* CurrentProject's name is java8
* 求小球落地5次後所經歷的路程和第5次反彈的高度
*/
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNextDouble()){
double hight = input.nextDouble();
// 定義數組,存儲每次彈起來的高度
double[] result = new double[5];
result[0] = hight / 2;
for (int i = 1; i < 5; i++) {
result[i] = result[i - 1] / 2;
}
double sum = hight;
for (int i = 0; i < result.length - 1; i++) {
sum += (result[i] * 2);
}
System.out.println(String.format("%.6f",sum));
System.out.println(String.format("%.6f", result[result.length - 1]));
}
}
}