题解 | #小球走过路程计算#
小球走过路程计算
http://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();
//write your code here......
float sum=h;
if(n==1){//只弹一次的情况
sum=h;
}
else{
for (int i = 2; i <= n; i++) {//弹两次及以上
sum += (float)(2* (h * Math.pow(0.5, i - 1)));第i次到第i-1次之间落地走过的距离
}
h=(float)(h * Math.pow(0.5,n));第n次的弹起高度
}
//输出格式为:System.out.println(String.format("%.3f", h)+" "+String.format("%.3f", sum));
System.out.println(String.format("%.3f", h)+" "+String.format("%.3f", sum));
}
}