Created
February 14, 2021 03:56
-
-
Save witmin/d02ad27b96b47e28b697a904f2a7f0ad to your computer and use it in GitHub Desktop.
2021春晚小学数学题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 2021年浙江卫视春晚让郭冬临喝了很多瓶水的小品里的一道数学题 | |
# 题面:小明有10元钱,一瓶饮料2元钱,2个瓶盖可以换一瓶饮料,4个瓶身可以换一瓶饮料 | |
# 问小明最多可以用这10元钱取得多少瓶饮料? | |
import math | |
# 持有的钱 | |
money = 10 | |
# 饮料单价 | |
price = 2 | |
# 瓶盖兑换规则 | |
cap_exchange_rate = 2 | |
# 瓶身兑换规则 | |
bottle_exchange_rate = 4 | |
# 买到的饮料总数 | |
total_bought = money / price | |
# 可通过积攒瓶盖瓶身兑换的饮料数 | |
def exchange_drinks(bought_drinks): | |
# 一开始的剩余瓶盖数为买到的饮料总数 | |
remain_caps = bought_drinks | |
# 一开始的剩余瓶身数为买到的饮料总数 | |
remain_bottles = bought_drinks | |
# 初始化可兑换的饮料数量 | |
total_exchanged_number = 0 | |
while remain_caps > 0 or remain_bottles > 0: | |
if remain_caps >= cap_exchange_rate or remain_bottles >= bottle_exchange_rate: | |
# 可兑换的瓶盖的数量 | |
cap_exchanged_number = int(math.floor(remain_caps / cap_exchange_rate)) | |
# 加总通过瓶盖兑换的饮料总数 | |
total_exchanged_number += cap_exchanged_number | |
# 剩余瓶盖 = 兑换后剩余的瓶盖余数 + 兑换的饮料带来的新饮料瓶盖 | |
remain_caps = int(remain_caps % cap_exchange_rate + cap_exchanged_number) | |
# 剩余的瓶子(来自瓶盖兑换带来的新瓶子) | |
remain_bottles += cap_exchanged_number | |
# 可兑换的瓶子的数量 | |
bottle_exchanged_number = int(math.floor(remain_bottles / bottle_exchange_rate)) | |
# 剩余的瓶子 | |
remain_bottles = int(remain_bottles % bottle_exchange_rate + bottle_exchanged_number) | |
# 剩余的瓶盖(来自瓶子兑换带来的新瓶盖) | |
remain_caps += bottle_exchanged_number | |
# 加总通过瓶子兑换的饮料总数 | |
total_exchanged_number += bottle_exchanged_number | |
else: | |
break | |
# 总的饮料数量 = 买到的饮料总数+兑换的饮料总数 | |
total = int(total_bought + total_exchanged_number) | |
print(total) | |
exchange_drinks(total_bought) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment