题解 | #求小球落地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); } }