题解 | #小球走过路程计算#
小球走过路程计算
https://www.nowcoder.com/practice/ddbb7021c0a7452f9044564234616913
import java.util.Scanner; import static java.lang.Math.pow; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 下落的高度和落地的次数 float h = scanner.nextFloat(); int n = scanner.nextInt(); float sum = 0; //write your code here...... float a = h; for (int i = 1; i <= n; i++) { if (i == 1) { sum = h; } else { h = a / (float) pow(2, i - 1); sum = sum + h * 2; } } float h1 = h / 2; System.out.println(String.format("%.3f", h1) + " " + String.format("%.3f", sum)); } }