一球从某一高度h落下单位米每次落地后反跳回原来高度的一半再落下。编程计算气球在第10次落地时共经过多少米 第10次反弹多高?输出包含两行第1行:到球第10次落地时一共经过的米数。第2行:第10次弹跳的高度。用C++
#include
int main() { double h; // 初始高度 cin >> h;
double totalDistance = h; // 初始高度即为第一次落地时经过的距离
double height = h; // 当前弹跳的高度
for(int i = 1; i <= 9; i++) {
totalDistance += height * 2; // 每次落地后,经过的距离为当前弹跳高度的两倍
height /= 2; // 当前弹跳高度减半
}
cout << totalDistance << endl;
cout << height << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/iKAT 著作权归作者所有。请勿转载和采集!