カフェのお客さんは 1 人 1〜5 個の星を付けます。1 行だけ書きましょう。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();
}
実行すると例で試せます。よさそうなら提出しましょう。