题解 | #数组分组#

数组分组

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

import itertools
if __name__ == "__main__":
    N = int(input())
    L = list(map(int,input().strip().split(" ")))
    # print(L)
    L3 = []
    L5 = []
    L0 = []
    for key in L:
        if key % 3 == 0:
            L3.append(key)
        elif key % 5 == 0:
            L5.append(key)
        else:
            L0.append(key)
    if sum(L3) == sum(L5):
            print("true")
    elif len(L0) == 0:
            print("false")
    else:
        S3 = sum(L3)
        S5 = sum(L5)
        S0 = sum(L0)
        flag = False
        for k in range(len(L0)):
            for perm in itertools.combinations(L0,k):
                # complimentary = 
                # sc = sum(complimentary)
                ss = sum(perm)
                sc = S0 - ss
                if S3 + sc == S5 + ss or S3 + ss == S5 + sc:
                    print("true")
                    # print(perm,complimentary,L0)
                    flag = True
                    break
            if flag == True:
                break
        if flag == False:
            print("false")

直接导包,用组合,注意不能用全排列,不然超时,渣渣骗分就这么爽,牛客没有说不能用itertools这个包啊

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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