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

买得起的

#024基础vector循环约 4 分钟

逛超市时只看买得起的东西。写 cheap_ones(prices, budget):把不超过 budget 的价钱按原来的顺序放进一个新的 vector 返回。一个都没有就返回空的。

示例
cheap_ones({3, 12, 5, 20}, 10)→[3, 5]
cheap_ones({8, 9}, 5)→[]
函数vector<int> cheap_ones(const vector<int> &prices, int budget)
main.cpp
#include <vector>
using namespace std;

vector<int> cheap_ones(const vector<int> &prices, int budget) {
    vector<int> picked;
    for (int p : prices) {
        // 在这里写:p 不超过 budget,就放进 picked
    }
    return picked;
}
Ctrl + Enter 也能运行
结果

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