球從 100 米高度自由下落,每次落地反彈回原高度一半,再落下。求第 10 次落地時,球總共經過的路程?第 10 次反彈高度?
h總路程 = 100 + 2*(50+25+...+第9次反彈高度) + 第10次下落高度
#include <stdio.h>
int main(void) {
double height = 100.0;
double total = height; // 第一次下落
for (int i = 1; i <= 10; ++i) {
height /= 2; // 反彈高度
if (i < 10) total += 2 * height; // 上升+下落
else total += height; // 第10次只算下落
}
printf("總路程: %.2f 米\n", total);
printf("第10次反彈高度: %.2f 米\n", height);
return 0;
}
總路程 = H + 2*H*(1 - (1/2)^9) = H * (3 - (1/2)^8)
第 n 次反彈高度 = H / 2^n
double bounce_total(double H, double restitution, int n) {
double total = H, h = H;
for (int i = 1; i <= n; ++i) {
h *= restitution;
total += (i < n) ? 2*h : h;
}
return total;
}