题解 | #数组分组#

数组分组

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

import sys

"""
评论区有些人用的数学做的,好强
递归:结果出现,在完全投入前及时止损,不需要完全遍历
"""

def fun(sum_3, sum_5, nums_other):
    if len(nums_other) == 0:
        if sum_3 == sum_5:
            return True
        else:
            return False
    else:
        fun1 = fun(sum_3 + nums_other[0], sum_5, nums_other[1::])
        fun2 = fun(sum_3, sum_5 + nums_other[0], nums_other[1::])
        return fun1 or fun2


n = int(input())
nums = list(map(int, input().split()))
nums_5 = []
nums_3 = []
nums_other = []
for i in nums:
    if i % 5 == 0:
        nums_5.append(i)
    elif i % 3 == 0:
        nums_3.append(i)
    else:
        nums_other.append(i)
sum_3 = sum(nums_3[::])
sum_5 = sum(nums_5[::])

print('true') if fun(sum_3, sum_5, nums_other) else print('false')

全部评论

相关推荐

09-28 09:18
吉首大学 Java
离上岸不远了的牛油很...:同27,你写的专业技能那些是真熟练了吗,我感觉稍微问深一点我都要🐔
你找实习最大的坎坷是什么
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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