题解 | 数组分组

数组分组

https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86



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


while True:
    try:
        n = int(input())
        nums = [int(i) for i in input().split()]
        other = []
        three = []
        five = []
        for i in nums:
            if i % 5 == 0:
                five.append(i)
            elif i % 3 == 0:
                three.append(i)
            else:
                other.append(i)
        res = func(three, five, other)
        if res:
            print("true")
        else:
            print("false")
    except:
        break

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务