Kodewick
开始学 →
基础←→0 / 60

咖啡店评分

#053基础常用算法约 4 分钟

咖啡店的评分,每人给 1 到 5 颗星。写一行:用 accumulate 把 stars 加起来放进 total。平均数是整数相除,去掉小数;没人评分就返回 0(模板里写好了)。

示例
average_stars({5, 4, 4})→4
average_stars({})→0
函数int average_stars(const vector<int> &stars)
main.cpp
#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();
}
Ctrl + Enter 也能运行
结果

写好了先点「运行」试试示例,没问题再点「提交」。