A bookshop gift card has balance on it. Write pay(amount): if there's enough, take it off and return true; if not, take nothing and return false (that last line is written). Exactly enough counts as enough.
card_left(50, {20, 40, 10})→20card_left(30, {})→30int card_left(int start, const vector<int> &buys)#include <vector>
using namespace std;
class GiftCard {
public:
int balance;
GiftCard(int start) {
balance = start;
}
bool pay(int amount) {
// your code here: if there's enough, pay and return true
return false;
}
};
int card_left(int start, const vector<int> &buys) {
GiftCard card(start);
for (int b : buys) {
card.pay(b);
}
return card.balance;
}
Run tries the examples. When they look right, Submit.