一个水瓶 Bottle,ml 是里面还有多少毫升水。写 drink(amount):喝一口,ml 就少 amount。每一口都不会比剩下的水多。
water_left(500, {100, 50})→350water_left(300, {})→300int water_left(int start, const vector<int> &sips)#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;
}
写好了先点「运行」试试示例,没问题再点「提交」。