手環記下了每天的步數。寫 total_steps(steps, n),把 n 天的步數加起來。迴圈讓指標 p 從第一天走到最後一天,你只要把 p 指著的數加進 total。n 是 0 時返回 0。
total_steps({3000, 4500, 5200}, 3)→12700total_steps({800}, 1)→800int total_steps(int *steps, int n)int total_steps(int *steps, int n) {
int total = 0;
for (int *p = steps; p < steps + n; p++) {
// 在這裡寫:把 p 指著的步數加進 total
}
return total;
}
寫好了先點「執行」試試示例,沒問題再點「提交」。