题解 | #求小球落地5次后所经历的路程和第5次反弹的高度#

求小球落地5次后所经历的路程和第5次反弹的高度

https://www.nowcoder.com/practice/2f6f9339d151410583459847ecc98446

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<double> hight;//记录每次下落的最大高度
    int start;
    double maxh, dist = 0;
    cin >> start;
    maxh = double(start);
    hight.push_back(maxh);
    for (int i = 1; i < 5; ++i)
    {
        hight.push_back(hight[i-1] / 2);
    }
    //计算经过的总路程到dist中
    dist = maxh;
    for (int i = 1; i < 5; ++i)
    {
        dist += 2 * hight[i];
    }
    //输出
    cout << dist << endl;
    cout << hight[4] / 2;//第五次反弹等于第五次下落最大高度的一半
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务