蛋糕柜里每块蛋糕是一个 struct Cake:名字和价钱。写 cheapest(cakes, n),返回最便宜那块的名字。至少有 1 块;一样便宜的,返回排在前面的。
cheapest({{"cheesecake", 28}, {"brownie", 12}, {"tiramisu", 30}}, 3)→'brownie'cheapest({{"donut", 6}}, 1)→'donut'const char *cheapest(struct Cake cakes[], int n)struct Cake {
const char *name;
int price;
};
const char *cheapest(struct Cake cakes[], int n) {
int best = 0;
for (int i = 1; i < n; i++) {
// 在这里写:这块比第 best 块便宜,best 就换成 i
}
return cakes[best].name;
}
写好了先点「运行」试试示例,没问题再点「提交」。