Kodewick
開始學 →
入門→0 / 86

加起來

#001入門for (int x : v)約 3 分鐘

寫 sum_all(v),把 vector 裡所有的數加起來返回。空的就是 0。

示例
sum_all({3, 4, 5})→12
sum_all({})→0
函式int sum_all(const vector<int> &v)
main.cpp
#include <vector>
using namespace std;

int sum_all(const vector<int> &v) {
    int total = 0;
    // 在這裡寫:用範圍 for 把每個數加到 total
    return total;
}
Ctrl + Enter 也能執行
結果

寫好了先點「執行」試試示例,沒問題再點「提交」。