Kodewick
開始學 →
基礎←→0 / 70

最便宜的蛋糕

#061基礎cakes[i].price <約 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 也能執行
結果

寫好了先點「執行」試試示例,沒問題再點「提交」。