一家人去旅行,每个箱子是一个 struct Bag:谁的、几公斤。写 total_kg(bags, n),把 n 个箱子的公斤数加起来。没有箱子返回 0。
total_kg({{"Ana", 18}, {"Ben", 23}}, 2)→41total_kg({{"Kai", 7}}, 1)→7int total_kg(struct Bag bags[], int n)struct Bag {
const char *owner;
int kg;
};
int total_kg(struct Bag bags[], int n) {
int total = 0;
for (int i = 0; i < n; i++) {
// 在这里写:把第 i 个箱子的 kg 加进 total
}
return total;
}
写好了先点「运行」试试示例,没问题再点「提交」。