题解 | #数组分组#
数组分组
https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86
import sys,itertools for line in sys.stdin: a = list(map(int,line.split())) grp1, grp2,grp3 = [], [], [] total = sum(a) / 2 n = len(a) for i in range(n): if a[i] % 3 == 0 and a[i] % 5 != 0: grp1.append(a[i]) elif a[i] % 5 == 0: grp2.append(a[i]) else: grp3.append(a[i]) res1 = total - sum(grp1) if res1 == 0: print('true') else: could = False for i in range(1,len(grp3)+1): com = itertools.combinations(range(len(grp3)),i) for k in com: summation = sum([grp3[j] for j in k]) if summation == res1: could = True if could: print('true') else: print('false')
思路:
首先按照要求拆分两组,将不属于任意一组的放在第三组,进行组合遍历,如果求和后能够满足条件,则输出true,否则false