题解 | #求小球落地5次后所经历的路程和第5次反弹的高度#
求小球落地5次后所经历的路程和第5次反弹的高度
http://www.nowcoder.com/practice/2f6f9339d151410583459847ecc98446
找规律就行
每次都是之前高度的一半
#include<iostream> #include<vector> using namespace std; vector<double> reboundHigh(double h, int n); int main(){ int h; int n = 5; while(cin >> h){ vector<double> v1 = reboundHigh(h, n); double sum = v1[0]; for(int i = 1; i < v1.size() - 1; ++i){ sum += v1[i] * 2; } cout << sum << endl << v1[v1.size() - 1] << endl; } return 0; } vector<double> reboundHigh(double h, int n){ vector<double> res(n + 1, 0); res[0] = h; for(int i = 1; i < res.size(); ++i){ res[i] = res[i - 1] /double(2); } return res; }