题解 | #小球走过路程计算#
小球走过路程计算
https://www.nowcoder.com/practice/ddbb7021c0a7452f9044564234616913
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); float h = scanner.nextFloat(); int n = scanner.nextInt(); // 每一次回弹后高度除2 double height = h / Math.pow(2, n); // 简单的等比数列求和 // 需要注意 除第一次下落 所有下落都会进行两倍的路程 double heightSumHalf = h / 2 * (1 - Math.pow(0.5, n - 1)) / (1 - 0.5); double heightSum = heightSumHalf * 2 + h; System.out.println(String.format("%.3f", height) + " " + String.format("%.3f", heightSum)); //输出格式为:System.out.println(String.format("%.3f", h)+" "+String.format("%.3f", sum)); } }