手环记下了每天的步数。写 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;
}
写好了先点「运行」试试示例,没问题再点「提交」。