Kodewick
开始学 →
入门←→0 / 60

水瓶

#040入门类和对象约 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 也能运行
结果

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