找规律就行
每次都是之前高度的一半
#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;
}
京公网安备 11010502036488号