Python程序教程

您现在的位置是:首页 >  Python

当前栏目

【python】分苹果

python,苹果
2025-03-18 08:48:42 时间

问题:一堆苹果,5个人。第一个人将苹果丢掉一个,然后平均分成5份后拿走其中的一份;第二个人将剩余的苹果丢掉一个,然后再平均分成5份后拿走其中的一份,依次类推…第五个人在第四个人拿走剩下的那部分苹果中同样丢掉一个,然后平均分成5份后拿走其中的一份。求问最少的苹果数。

depth = 0
  
def match(num):
    """
    """
    global depth
    if (num - 1) % 5 == 0:
        depth = depth + 1
        if depth == 5:
            return True
        return match(num - (num - 1)/5)
    return False

def findMagicNum():
    """
    """
    magic = 5
    while (1):
        global depth
        depth = 0
        if match(magic):
            return magic
        else:
            magic = magic + 1

if __name__ == "__main__":
    magic = findMagicNum()
    print("magic is %s" % magic)

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/184388.html原文链接:https://javaforall.cn