[英]find all possible combinations of N non-repeating numbers within a certain range that add up to X
[英]Algorithm to find combinations of sets of numbers that add up to equal or greater than X
所以我试图解决的问题是我想在我拥有的集合中找到可能的数字组合,这些组合加起来为 400
我在第 1 组中有 6 个数字,它们可以是以下任何一个:[0, 6, 8, 12, 16, 30] 和第 2 组中的另外 6 个数字,它们可以是以下任何一个:[0, 16, 20, 25, 32, 40, 50]
我认识到,因为我所关心的只是总和,所以顺序应该无关紧要,但我不确定我实际上会如何进行设置。 一次改变 1 个数字,每次迭代减少值,然后一旦我达到总数低于 400,打破该运行并移动到下一个数字是否有意义? 例如:如果我从 [30,30,30,30,30,30][50,50,50,50,50,50] 开始减少最后一个索引,一旦我点击 [30,30,30,30 ,30,30][50,50,50,32,20,16] 其余的迭代集中在第 11 列(只有一列,当它等于 16 时)可以忽略,因为它们只会减少。 或者有没有更合乎逻辑的方法来解决这个问题?
您可以订购两组,为第一组中的每个 num 设置一个循环,然后将其与第二组中的每个 num 相加,但是一旦您的总和未能超过 400,您就可以停止。
就像是
numbers_whose_sums_are_bigger_than_the_number_in_question = []
for x in ordered_set_a:
sum = 401
while sum > 400:
for y in ordered_set_b:
sum = x + y
if sum > 400:
numbers_whose_sums_are_bigger_than_the_number_in_question.append([x,y])
print(numbers_whose_sums_are_bigger_than_the_number_in_question)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.