题解 | #数组分组#
数组分组
http://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86
while True:
try:
a = int(input())
b = list(map(int,input().split()))
g1 = []
g2 = []
g3 = []
for i in b:
if i % 5 == 0:
g1.append(i)
#print(g1)
else:
if i % 3 == 0:
g2.append(i)
else:
g3.append(i)
#print(g3)
def group(g1, g2, g3):
#g = [g1, g2]
#print(g[0])
if len(g3) == 0:
#print(g1)
return sum(g1) == sum(g2)
else:
for j in g3:
p = g1 + [j]
q = g2 + [j]
g3 = g3[1:] #!!!!!注意这里不能用remove否则会依次缩小选择!每一次选择都基于之前的选择
#比如用例 3 8 8 5 14用remove并不能通过。g3[1:]的写法保证每次都是单独的判断!互不相关
#print(g3)
if group(p,g2,g3) or group(g1,q,g3):
return True
else:
return False
#for i in range(len(g)):
# rest = g[:i] + g[i+1:]
# chosen = g[i]
#print(chosen)
#for j in g3:
#chosen.append(j)
#print(chosen)
# g3.remove(j)
# print(g3)
# if group(chosen.append(j), rest, g3):# or group(chosen, rest.append(j) ,g3):
# return True
#else:
# return False
#return False
if group(g1, g2, g3):
print('true')
else:
print('false')
except:
break