咖啡店的评分,每人给 1 到 5 颗星。写一行:用 accumulate 把 stars 加起来放进 total。平均数是整数相除,去掉小数;没人评分就返回 0(模板里写好了)。
average_stars({5, 4, 4})→4average_stars({})→0int average_stars(const vector<int> &stars)#include <numeric>
#include <vector>
using namespace std;
int average_stars(const vector<int> &stars) {
if (stars.empty()) {
return 0;
}
int total = 0;
// 在这里写:用 accumulate 算 total
return total / stars.size();
}
写好了先点「运行」试试示例,没问题再点「提交」。