其他分享
首页 > 其他分享> > 郭冬临到底喝了多少水?

郭冬临到底喝了多少水?

作者:互联网

题目

2021浙江春晚,郭冬临到底喝了多少水?

2个瓶盖 换1瓶, 4个空瓶 换1瓶,请问5,能喝到多少汽水?

解答

写了2种

all_water_num = 0  # 喝水总数
empty_bottle = 0  # 空瓶总数
capsule = 0  # 瓶盖
def exchange(num):

    global all_water_num
    global empty_bottle
    global capsule
    # 空瓶和瓶盖需要累加
    empty_bottle += num
    capsule += num
    all_water_num += num

    if empty_bottle < 4 and capsule < 2: return all_water_num

    water_num = 0  # 能兑换的数量
    water_num += empty_bottle // 4
    water_num += capsule // 2
    # 更新空瓶和瓶盖
    empty_bottle = empty_bottle % 4
    capsule = capsule % 2
    return exchange(water_num)
def run2(num):


    def exchange2(empty_bottle, capsule, all_water_num):
        if empty_bottle < 4 and capsule < 2: return all_water_num
        water_num = 0
        water_num += empty_bottle // 4
        water_num += capsule // 2
        empty_bottle = empty_bottle % 4
        capsule = capsule % 2

        all_water_num += water_num
        empty_bottle += water_num
        capsule += water_num
        return exchange2(empty_bottle, capsule, all_water_num)
    return exchange2(num, num, num)

标签:return,bottle,到底,water,capsule,num,郭冬临,多少,empty
来源: https://www.cnblogs.com/wfer/p/14397735.html