Kodewick
开始学 →
小挑战←0 / 70

拿零花钱买菜

#070小挑战数组条件约 6 分钟

带着 money 元去买菜,按清单的顺序买:钱够就买下(钱变少),不够就跳过这一样,接着看下一样。写 items_bought(prices, n, money),返回一共买到几样。

示例
items_bought({5, 12, 3, 8}, 4, 20)→3
items_bought({}, 0, 10)→0
函数int items_bought(int prices[], int n, int money)
main.c
int items_bought(int prices[], int n, int money) {
    int bought = 0;
    for (int i = 0; i < n; i++) {
        // 在这里写:钱够就买:money 减去价钱,bought 加 1
    }
    return bought;
}
Ctrl + Enter 也能运行
结果

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