题解 | #数组分组#
数组分组
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')

查看14道真题和解析
传音控股晋升空间 52人发布