题解 | #数组分组#

数组分组

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

# 其实递归的想法很单纯,但是就是会超时,优化了好久最后发现一开始做所有输入数字之和的奇偶判断可以省很多时间
# 本来想看题解,还是强行自己优化出来了,6
import sys

ipt = []

for line in sys.stdin:
    nums = list(map(int, line.strip().split()))

    ipt.append(nums)

nums = ipt[1]

three, five, other = [], [], []

# print(nums, three, five, other)

for num in nums:
    if num % 3 == 0:
        three.append(num)
    elif num % 5 == 0:
        five.append(num)
    else:
        other.append(num)

three, five = sum(three), sum(five)

other.sort() if other else None

# print(nums, three, five, other)

def judge(three, five, other):
    other_set = list(set(other))
    # print(three, five, other_set)
    if other:
        if three == five and sum(other) == 0:
            return True
        elif three == five and sum(other) != 0:
            return False
        elif three != five and (three + sum(other) == five or five + sum(other) == three):
            return True
        for i in range(len(other_set)):
            # print(i, other[i], three, five)
            another = other[:]
            c = another.pop(i)
            if i > 0 and other[i-1] == c:
                continue
            res = judge(three+c, five, another) or judge(three, five+c, another)
            if res:
                return True
        
    else:
        if three == five:
            return True
        else:
            return False

if (three + five + sum(other)) % 2 != 0:
    res = False
else:
    res = judge(three, five, other)

# print(res, 1)

if res:
    print('true')
else:
    print('false')

全部评论

相关推荐

11-14 16:13
已编辑
重庆科技大学 测试工程师
Amazarashi66:不进帖子我都知道🐮❤️网什么含金量
点赞 评论 收藏
分享
Yushuu:你的确很厉害,但是有一个小问题:谁问你了?我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了😆
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务