题解 | #求小球落地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 { public static void main(String[] args) { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); StringBuilder ans = new StringBuilder(); String a; try { a = r.readLine(); } catch (IOException e) { throw new RuntimeException(e); } char[] h = a.toCharArray(); int i = 0, l = h.length; float hei = 0.0f, tol5; do {//获得高度,浮点数 hei *= 10; hei += h[i] - '0'; i++; } while (i < l); i = 1; tol5 = -hei;//第0次落地时,球经过的米数为-h,球的开始点是地面 do { tol5 += hei * 2; hei /= 2; i++; } while (i < 6); ans.append(tol5).append("\n").append(hei); System.out.print(ans); } }