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

收盘子

#039小挑战引用vector约 6 分钟

洗好的盘子摞在架子上,一个个从最上面拿,摞进柜子。写 move_one(from, to):把 from 的最后一个拿出来,放到 to 的最后。from 是空的就什么也不做。两个参数都是引用,两边都会变。

示例
stacked({"blue", "white", "red"}, 2)→['red', 'white']
stacked({"blue"}, 3)→['blue']
函数vector<string> stacked(vector<string> rack, int n)
main.cpp
#include <string>
#include <vector>
using namespace std;

void move_one(vector<string> &from, vector<string> &to) {
    // 在这里写:from 的最后一个搬到 to
}

vector<string> stacked(vector<string> rack, int n) {
    vector<string> cupboard;
    for (int i = 0; i < n; i++) {
        move_one(rack, cupboard);
    }
    return cupboard;
}
Ctrl + Enter 也能运行
结果

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