Kodewick
开始学 →
基础←→0 / 70

最便宜的蛋糕

#061基础结构体数组约 5 分钟

蛋糕柜里每块蛋糕是一个 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)
main.c
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;
}
Ctrl + Enter 也能运行
结果

写好了先点「运行」试试示例,没问题再点「提交」。