酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

description 汤汤开了一家咖啡店,在研发新品的时候,她发现不同品牌的牛奶味道不一样,拿铁的味道也会有微妙的区别,而且鲜牛奶的口感会比常温奶更好。经过精心的筛选,她最终选定了两家巴氏杀菌鲜牛奶供应商。每隔一段时间。小汤都会去市场购买当天最新鲜的牛奶,但由于供应商的库存和价格会有波动,每次进货的数量并不固定。 品牌a的鲜牛奶是塑料瓶包装,保质期只有7天;品牌b的鲜牛奶是专用纸盒包装,保质期有15天;为了减少浪费,汤汤会优先使用临近保质期的鲜牛奶,而过了保质期的牛奶,就只能处理掉了。 汤汤会记下每次进货的时间和数量以及每天的消耗量,以方便盘点库存。不用考虑每日消耗量超过剩余量的情况哦,汤汤会提前采购的~ input 第一行输入整数 n,表示有n条操作记录; 之后n行,每行是一个操作,操作格式如下: 1 purchas 10 20 // 开店第1天,购买了a品牌鲜奶10l,b品牌鲜奶20l 2 cost 10 // 开店第2天,消耗鲜奶10l 3 check // 开店第3天,盘点 output 第一行,输出a品牌牛奶的盘点结果,包括库存量

以下是使用 Python 实现的解决方案: ```python class MilkInventory: def __init__(self): self.milk_a_quantity = 0 self.milk_a_purchase_dates = {} self.milk_b_quantity = 0 self.milk_b_purchase_dates = {} def purchase(self, brand_a_quantity, brand_b_quantity): current_day = 1 # 假设当前是第一天 self.milk_a_quantity += brand_a_quantity self.milk_a_purchase_dates[current_day] = brand_a_quantity self.milk_b_quantity += brand_b_quantity self.milk_b_purchase_dates[current_day] = brand_b_quantity def consume(self, quantity): if self.milk_a_quantity + self.milk_b_quantity >= quantity: if self.milk_a_quantity >= quantity: self.milk_a_quantity -= quantity else: remaining_quantity = quantity - self.milk_a_quantity self.milk_a_quantity = 0 self.milk_b_quantity -= remaining_quantity def check_inventory(self): current_day = 3 # 假设当前是第三天 remaining_milk_a = self.milk_a_quantity for day, quantity in self.milk_a_purchase_dates.items(): if current_day - day >= 7: remaining_milk_a -= quantity print(f"品牌 A 牛奶的库存量为: {remaining_milk_a}") inventory = MilkInventory() n = 3 operations = [ "1 Purchas 10 20", "2 cost 10", "3 check" ] for operation in operations: parts = operation.split() if parts[1] == "Purchas": inventory.purchase(int(parts[2]), int(parts[3])) elif parts[1] == "cost": inventory.consume(int(parts[2])) elif parts[1] == "check": inventory.check_inventory() ``` [2024-10-31 21:32:27 | AI写代码神器 | 474点数解答]

相关提问