My grandfather always predicted how old people would get, and right before he passed away he revealed his secret!
In honor of my grandfather’s memory we will write a function using his formula!
- Take a list of ages when each of your great-grandparent died.
- Multiply each number by itself.
- Add them all together.
- Take the square root of the result.
- Divide by two.
Example
predictAge(65, 60, 75, 55, 60, 63, 64, 45) === 86
Note: the result should be rounded down to the nearest integer.
分析
此题目难点不在求解的思路上. 而是面对多个参数时如何利用语言本身的语法特性来简化代码.
// 最笨的解法:
#include <math.h>
int predictAge(int age1, int age2, int age3, int age4, int age5, int age6, int age7, int age8)
{
return sqrt(age1*age1+age2*age2+age3*age3+age4*age4+age5*age5+age6*age6+age7*age7+age8*age8)/2;
}
更好的解法
重点:
- 用
std::array<类型, 个数>
来替代C数组, 以此获取使用模板的能力 - typename后加省略号
- 类型名后加省略号
sizeof...()
- 对后续无须改变的变量一律用
const
修饰 std::common_type_t<T...>
返回公共最小父类型std::accumulate
对数组中的值进行累加
#include <numeric> // std::accumulate()
#include <cmath> // std::sqrt()
#include <array> // std::array
template <typename ... T>
int predictAge(const T ...ages)
{
const std::array<std::common_type_t<T...>, sizeof...(T)> all_ages = { (ages*ages)... };
const int sum = { std::accumulate(all_ages.begin(), all_ages.end(), 0) }; // 初始值为0
const auto squared = std::sqrt(sum) / 2;
return squared;
}