python之可换多少汽水瓶

 

'''
有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,
方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让
老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝?
'''

numOfBottle = int(input("Please input bottle number:"))

def calcBottle(numOfEmptyBottle):
    numOfChangeBottle = 0
    numOfLeftEmptyBottle = numOfEmptyBottle

    while 1:
        if numOfLeftEmptyBottle == 0:
            numOfChangeBottle = numOfChangeBottle
            numOfLeftEmptyBottle = 0

        if numOfLeftEmptyBottle == 1:
            numOfChangeBottle = numOfChangeBottle
            numOfLeftEmptyBottle = 1

        if numOfLeftEmptyBottle == 2:
            numOfChangeBottle = numOfChangeBottle + 1
            numOfLeftEmptyBottle = 0

        if numOfLeftEmptyBottle > 2:
            numOfChangeBottle = numOfChangeBottle + numOfLeftEmptyBottle // 3
            numOfLeftEmptyBottle = numOfLeftEmptyBottle % 3 + numOfLeftEmptyBottle // 3

        if numOfLeftEmptyBottle < 2:
            break

    return numOfChangeBottle

print(calcBottle(numOfBottle))

 

posted @ 2020-02-24 16:44  水里的芋头  阅读(597)  评论(0编辑  收藏  举报