递归 | HJ93 数组分组

def func(three, five, other):
    if not other:
        if sum(three) == sum(five):
            return True
        return False
    if func(three+[other[0]], five, other[1:]):
        return True
    if func(three, five+[other[0]], other[1:]):
        return True
    return False

while True:
    try:
        n = input()
        nums = input().split()
        a1, a2, a3 = [], [], []
        for num in nums:
            num = int(num)
            if num % 3 == 0:
                a1.append(num)
            elif num % 5 == 0:
                a2.append(num)
            else:
                a3.append(num)
        if func(a1, a2, a3):
            print('true')
        else:
            print('false')
    except:
        break

用时:2h+20min

华为笔试刷题 文章被收录于专栏

高质量题: 1~40:HJ16,HJ22,HJ24,HJ26,HJ27,HJ28,HJ35,HJ37,HJ39; 40~80:HJ41,HJ42,HJ43,HJ44,HJ48,HJ50,HJ52,HJ53,HJ57,HJ61,HJ63,HJ64,HJ70,HJ71,HJ74,HJ77; 80~108:HJ82,HJ85,HJ88,HJ89,HJ93,HJ95,HJ98,HJ103,HJ107

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务