Kodewick
開始學 →
小挑戰←→0 / 86

收盤子

#039小挑戰back() / pop_back()約 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 也能執行
結果

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