简体   繁体   English

查找加起来等于或大于 X 的数字集组合的算法

[英]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.

相关问题 在一定范围内找到N个非重复数字的所有可能组合,这些总数加起来为X - find all possible combinations of N non-repeating numbers within a certain range that add up to X 如何找到小于或等于X的最大值和大于或等于X的最小值? - How do I find the largest value smaller than or equal to X and the smallest value greater than or equal to X? 查找一组素数大于x的素数的算法 - algorithm to find products of a set of primes, in order, greater than x 找出一组中的哪些数字组合加起来等于给定的总数 - Find out which combinations of numbers in a set add up to a given total 算法总结所有组合的数字列表 - algorithm to sum up a list of numbers for all combinations 仅使用集合中的数字找到等于或大于给定目标的总和 - Find a sum equal or greater than given target using only numbers from set 给定一个已排序的数组和一个参数 k,求线性时间内大于或等于 k ​​的两个数之和的计数 - Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k in linear time 如何找到从多个列表中选择大于或等于给定数字的数字的可能方法的数量? - How to find number of possible ways to pick numbers greater than or equal to a given number from multiple lists? 用于查找数字集合的算法是否可以加起来X,具有约束直方图 - Algorithm for finding if set of numbers can add up to X, with constrained histogram 查找大于或等于z(正整数,可能是2的幂)的倍数的最小整数(正整数)。 - Find smallest integer greater or equal than x (positive integer) multiple of z (positive integer, probably power of 2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM