代码拉取完成,页面将自动刷新
#include <iostream>
#include <memory>
class Discount {
public:
virtual ~Discount() = default;
virtual void apply(int price) = 0;
};
class TenPercentDiscount : public Discount {
public:
void apply(int price) override {
std::cout << static_cast<int>(price * 0.9) << std::endl;
}
};
class FullDiscount : public Discount {
public:
void apply(int price) {
if (price >= 300) {
price -= 40;
} else if (price >= 200) {
price -= 25;
} else if (price >= 150) {
price -= 15;
} else if (price >= 100) {
price -= 5;
}
std::cout << price << std::endl;
}
};
class Context {
public:
void setDiscount(Discount *discount) {
m_discount = std::unique_ptr<Discount>(discount);
}
void applyDiscount(int price) {
m_discount->apply(price);
}
private:
std::unique_ptr<Discount> m_discount;
};
int main() {
int n;
std::cin >> n;
while (n--) {
int price, type;
std::cin >> price >> type;
Context context;
if (type == 1) {
context.setDiscount(new TenPercentDiscount());
} else if (type == 2) {
context.setDiscount(new FullDiscount());
}
context.applyDiscount(price);
}
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。