以下是使用 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点数解答]