题解 | #数组分组#
数组分组
http://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86
```
#本题最终转换为使用dfs,求解在整形数组中,是否存在和为某定值的组合
import sys
#i:步进,sumol:除去被3、5整除之外的数列表,target= sumall // 2 - sum5,res:当前测试的组合, s:组合的和
def dfs(i, sumol, target, res, s):
found = False
if s == target:
# print(res)
found = True
else:
for j in range(i, len(sumol)):
res.append(sumol[j])
# print(res)
found = dfs(j + 1, sumol, target, res, s + sumol[j])
if found == True:
break
res.pop()
return found
while True:
try:
N = int(input())
nums = list(map(int, input().split()))
sum5, sum3, sumall, sumol = 0, 0, 0, []
for i in nums:
sumall += i
if i % 5 == 0:
sum5 += i
elif i % 3 == 0:
sum3 += i
else:
sumol.append(i)
if sumall % 2 != 0:#不能均分的,一定是不符合条件了
print("false")
else:
res = [] #当前测试的组合
#左右两边都是总和的一半.减去sum5,就是组合的目标和
#求解在整形数组中,是否存在和为某定值的组合
target = sumall // 2 - sum5
if dfs(0, sumol, target, res, 0):
print("true")
else:
print("false")
except:
# print(sys.exc_info())
break
# #使用dfs,搜索使sum5 + x = sum3 + y的组合
# import sys
# def dfs(sum5, sum3, i, sumol, sumo, res):
# found = False
# if sum5 + sum(res) == sum3 + sumo - sum(res):
# # print(res)
# found = True
# else:
# for j in range(i, len(sumol)):
# res.append(sumol[j])
# found = dfs(sum5, sum3, j + 1, sumol, sumo, res)
# if found == True:
# break
# res.pop()
# return found
# while True:
# try:
# N = int(input())
# nums = list(map(int, input().split()))
# sum5, sum3, sumo, sumol = 0, 0, 0, []
# for i in nums:
# if i % 5 == 0:
# sum5 += i
# elif i % 3 == 0:
# sum3 += i
# else:
# sumol.append(i)
# sumo += i
# res = []
# if (sum3 + sum5 + sumo) % 2 != 0:
# print("false")
# else:
# if dfs(sum5, sum3, 0, sumol, sumo, res):
# print("true")
# else:
# print("false")
# except:
# # print(sys.exc_info())
# break
# #输入两个整数n和m,从数列1,2,3..n中随意取几个数,使其和等于m,要求将其中所有可能的组合列出来
# import sys
# def dfs(i, nums, res, s, t):
# # print(res)
# if s == t:
# print(res,":" , t)
# return
# else:
# for j in range(i, len(nums)):
# res.append(nums[j])
# dfs(j + 1, nums, res, s + nums[j], t)
# res.pop()
# while True:
# try:
# n, m = map(int, input().split())
# nums = [i for i in range(1, n+1)]
# res = []
# dfs(0, nums, res, 0, m)
# except:
# # print(sys.exc_info())
# break
import sys
#i:步进,sumol:除去被3、5整除之外的数列表,target= sumall // 2 - sum5,res:当前测试的组合, s:组合的和
def dfs(i, sumol, target, res, s):
found = False
if s == target:
# print(res)
found = True
else:
for j in range(i, len(sumol)):
res.append(sumol[j])
# print(res)
found = dfs(j + 1, sumol, target, res, s + sumol[j])
if found == True:
break
res.pop()
return found
while True:
try:
N = int(input())
nums = list(map(int, input().split()))
sum5, sum3, sumall, sumol = 0, 0, 0, []
for i in nums:
sumall += i
if i % 5 == 0:
sum5 += i
elif i % 3 == 0:
sum3 += i
else:
sumol.append(i)
if sumall % 2 != 0:#不能均分的,一定是不符合条件了
print("false")
else:
res = [] #当前测试的组合
#左右两边都是总和的一半.减去sum5,就是组合的目标和
#求解在整形数组中,是否存在和为某定值的组合
target = sumall // 2 - sum5
if dfs(0, sumol, target, res, 0):
print("true")
else:
print("false")
except:
# print(sys.exc_info())
break
# #使用dfs,搜索使sum5 + x = sum3 + y的组合
# import sys
# def dfs(sum5, sum3, i, sumol, sumo, res):
# found = False
# if sum5 + sum(res) == sum3 + sumo - sum(res):
# # print(res)
# found = True
# else:
# for j in range(i, len(sumol)):
# res.append(sumol[j])
# found = dfs(sum5, sum3, j + 1, sumol, sumo, res)
# if found == True:
# break
# res.pop()
# return found
# while True:
# try:
# N = int(input())
# nums = list(map(int, input().split()))
# sum5, sum3, sumo, sumol = 0, 0, 0, []
# for i in nums:
# if i % 5 == 0:
# sum5 += i
# elif i % 3 == 0:
# sum3 += i
# else:
# sumol.append(i)
# sumo += i
# res = []
# if (sum3 + sum5 + sumo) % 2 != 0:
# print("false")
# else:
# if dfs(sum5, sum3, 0, sumol, sumo, res):
# print("true")
# else:
# print("false")
# except:
# # print(sys.exc_info())
# break
# #输入两个整数n和m,从数列1,2,3..n中随意取几个数,使其和等于m,要求将其中所有可能的组合列出来
# import sys
# def dfs(i, nums, res, s, t):
# # print(res)
# if s == t:
# print(res,":" , t)
# return
# else:
# for j in range(i, len(nums)):
# res.append(nums[j])
# dfs(j + 1, nums, res, s + nums[j], t)
# res.pop()
# while True:
# try:
# n, m = map(int, input().split())
# nums = [i for i in range(1, n+1)]
# res = []
# dfs(0, nums, res, 0, m)
# except:
# # print(sys.exc_info())
# break
```