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

麵包漲價

#036基礎for (int &p : prices)約 4 分鐘

麵包店的麵包都要漲 extra 元。寫 raise_all(prices, extra):用範圍 for 把 prices 裡每個價錢都加上 extra。沒有面包(空的)就什麼都不變。

示例
new_prices({10, 12, 8}, 2)→[12, 14, 10]
new_prices({}, 5)→[]
函式vector<int> new_prices(vector<int> prices, int extra)
main.cpp
#include <vector>
using namespace std;

void raise_all(vector<int> &prices, int extra) {
    // 在這裡寫:每個價錢加上 extra
}

vector<int> new_prices(vector<int> prices, int extra) {
    raise_all(prices, extra);
    return prices;
}
Ctrl + Enter 也能執行
結果

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