带着 money 元去买菜,按清单的顺序买:钱够就买下(钱变少),不够就跳过这一样,接着看下一样。写 items_bought(prices, n, money),返回一共买到几样。
items_bought({5, 12, 3, 8}, 4, 20)→3items_bought({}, 0, 10)→0int items_bought(int prices[], int n, int money)int items_bought(int prices[], int n, int money) {
int bought = 0;
for (int i = 0; i < n; i++) {
// 在这里写:钱够就买:money 减去价钱,bought 加 1
}
return bought;
}
写好了先点「运行」试试示例,没问题再点「提交」。