使用Python先排序再直接暴力搜索即可
def fin(a, h): i = 0 if h == 0: return True while i < len(a) and a[i] <= h: tmp = a[i] a.pop(i) if fin(a, h - tmp): return True a[i:i] = [tmp] i += 1 return False a = input().split(' ') sum = 0 for i in range(len(a)): a[i] = int(a[i]) sum += a[i] h = sum % 7 a.sort() while h < sum: if fin(a, h): print(sum - h) break h += 7 if(h > sum): print(-1)
全部评论
(5) 回帖