Kodewick
開始學 →
入門←→0 / 86

水瓶

#040入門void drink(int amount)約 3 分鐘

一個水瓶 Bottle,ml 是裡面還有多少毫升水。寫 drink(amount):喝一口,ml 就少 amount。每一口都不會比剩下的水多。

示例
water_left(500, {100, 50})→350
water_left(300, {})→300
函式int water_left(int start, const vector<int> &sips)
main.cpp
#include <vector>
using namespace std;

class Bottle {
public:
    int ml;

    Bottle(int start) {
        ml = start;
    }

    void drink(int amount) {
        // 在這裡寫:ml 少 amount
    }
};

int water_left(int start, const vector<int> &sips) {
    Bottle b(start);
    for (int s : sips) {
        b.drink(s);
    }
    return b.ml;
}
Ctrl + Enter 也能執行
結果

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