题解 | #求小球落地5次后所经历的路程和第5次反弹的高度#
求小球落地5次后所经历的路程和第5次反弹的高度
https://www.nowcoder.com/practice/2f6f9339d151410583459847ecc98446
#include <iostream>
#include <list>
using namespace std;
int main() {
double initial_height;
cin >> initial_height;
/* 5次反弹 */
int n = 5;
/* 记录每一次反弹高度 */
list<double> bunce_height_list;
double bunce_height = initial_height;
for(int i = 0; i < n; i++)
{
/* 每次*0.5 */
bunce_height*=0.5;
bunce_height_list.push_back(bunce_height);
}
double move_len = 0;
double up_len = 0;
if(n == 1)
{
move_len = initial_height; /* 第一次落地时,共经历多少米 */
up_len = bunce_height_list.front(); /* 第一次反弹的高度 */
}
else {
auto pos = bunce_height_list.begin();
/* 初始自由下落经历长度 */
move_len = initial_height;
for(int i = 0; i < n - 1; i++)
{
/* 每一次触底->反弹->触底,经历长度为 反弹高度 * 2 */
move_len += ((*pos) * 2);
pos++;
}
/* 反弹高度 */
// up_len = bunce_height_list.back();
up_len = *pos;
}
cout << move_len << '\n';
cout << up_len;
}
// 64 位输出请用 printf("%lld")