ボールが height メートルの高さから落ち、着地するたびに元の高さの半分まで跳ね返って、また落ちます。times 回目に着地したとき、ボールが全部で何メートル動いたかを返す total_distance(height, times) を書きましょう。たとえば 100 メートルから落とすと、1 回目の着地までに 100 メートル。50 メートル跳ねて落ち、2 回目の着地で合計 200 メートルです。
total_distance(100, 10)→299.609375total_distance(100, 1)→100.0total_distance(100, 3)→250.0double total_distance(double height, int times)double total_distance(double height, int times) {
double total = height;
double h = height / 2;
for (int i = 2; i <= times; i++) {
// ここにコードを書く:今回跳ねて落ちた距離を足し、跳ねる高さを半分にする
}
return total;
}
実行すると例で試せます。よさそうなら提出しましょう。