题解 | #数组分组#
数组分组
https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86
def solution(nums): Sum5 = Sum3 = 0 tmp = [] while nums: num = nums.pop() if num%5==0: Sum5 += num elif num%3==0: Sum3 += num else: tmp.append(num) sub = abs(Sum5 - Sum3) if not tmp: return False if sub else True pf = sorted(tmp) for i in range(1,len(tmp)): pf[i] += pf[i-1] Sumt = pf[-1] if sub==0: if not Sumt: return True if pf[-1]%2==0 and (pf[-1]/2) in pf: return True return False return True if findSub(tmp,sub) else False def findSub(nums,target): def dfs(nums,S1,S2): if not nums: return True if abs(S1-S2) == target else False num = nums.pop() return dfs(nums[:],S1+num,S2) or dfs(nums[:],S1,S2+num) return dfs(nums,0,0) n = int(input()) nums = list(map(int,input().split())) if solution(nums): print("true") else: print("false")
#我拿到offer啦##好的APP推送需要满足什么条件##你觉得一款游戏为什么好玩#