[英]Algorithm to find combinations of sets of numbers that add up to equal or greater than X
So the problem I'm trying to solve is that I would like to find the possible combinations of numbers in the sets that I have that add up to 400所以我试图解决的问题是我想在我拥有的集合中找到可能的数字组合,这些组合加起来为 400
I have 6 numbers in set 1 that can be any of the following: [0, 6, 8, 12, 16, 30] and another 6 numbers in set 2 that can be any of the following: [0, 16, 20, 25, 32, 40, 50]我在第 1 组中有 6 个数字,它们可以是以下任何一个:[0, 6, 8, 12, 16, 30] 和第 2 组中的另外 6 个数字,它们可以是以下任何一个:[0, 16, 20, 25, 32, 40, 50]
I recognize that since all I'm concerned about is the sum, order should not matter, but I'm not sure how I'd actually approach the set up.我认识到,因为我所关心的只是总和,所以顺序应该无关紧要,但我不确定我实际上会如何进行设置。 Would it make sense to approach it changing 1 number at a time, decreasing in value for each iteration, then once I hit a total under 400 breaking that run and moving to the next number?
一次改变 1 个数字,每次迭代减少值,然后一旦我达到总数低于 400,打破该运行并移动到下一个数字是否有意义? For example: If I started with [30,30,30,30,30,30][50,50,50,50,50,50] decreasing in the last index, once I hit [30,30,30,30,30,30][50,50,50,32,20,16] the rest of the iterations focused on that 11th column (there's only one more and that's when its equal to 16) could be ignored because they only decrease.
例如:如果我从 [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 时)可以忽略,因为它们只会减少。 Or is there a more logical method to approach this with?
或者有没有更合乎逻辑的方法来解决这个问题?
You can order both sets, set a loop for each num in the first set to be summed to every num in the second but you can stop once your sum fails to be over 400.您可以订购两组,为第一组中的每个 num 设置一个循环,然后将其与第二组中的每个 num 相加,但是一旦您的总和未能超过 400,您就可以停止。
Something like就像是
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.