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

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

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

// 这很明显就是一个递归,递归的参数就是次数其实高度以及最后的长度
#include<stdio.h>
#include<iostream>
#include<vector>

using namespace std;



pair<float, float> dfs(float height , int count, float res) {
//     cout<<"h: "<<height<<" c: "<<count<<" res: "<<res<<endl;
    if(count == 0) {
        return {height,res};
    }

    res+=height+height/2;
    count--;
    return dfs(height/2,count,res);


}


int main() {
    float height;

    while(cin>>height) {
//         cout<<height<<endl;
        auto res = dfs(height,5,0.f);
        printf("%g \n",res.second-res.first);
//         cout<<res.second-res.first<<endl;
        printf("%0.5f",res.first);

    }

}
全部评论

相关推荐

07-01 17:14
中北大学 Java
兄弟们是真是假
牛客46374834...:我在boss上投java岗从来没成功过
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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