问题描述
我在excel中使用VB,并且我在A列中有一个数字列表,我正在尝试查找哪些数字匹配来计算另一个数字的总和。数字列表非常冗长,可能有多个组合。
Ex。
List = 1 2 3 4 5 6
Sum = 10
输出= 1,2,3,4和1,3,6和1,4,5和2,3,5和4,6
任何想法?
I am using VB in excel and I have a list of numbers in column A and I am trying to find which numbers match up to make a sum of another number. The list of numbers is quite lengthy, and there may be more then one combination.
Ex.
List = 1 2 3 4 5 6
Sum = 10
output = 1,2,3,4 and 1,3,6 and 1,4,5 and 2,3,5 and 4,6
Any ideas?
推荐答案
它不像你想象的那么容易。一个不太难的方法(但不是非常有效)将创建所有posibles组合:2 ^ n,并检查满足总和的那些。
我将制作2 ^的矩阵nx 2,对于每个组合,我们将得到组合及其总和,然后如果它与您的数字匹配则按总和检查总和。
例如
列表: 1,2,3
sum = 3
矩阵将是2 ^ 3 = 8 x 2(实际上是7x2,因为我们不会考虑空的)
矩阵看起来像他的:
Its not as easy as you think. A not very hard way (but not very efficient) will be creating all the posibles combinations: 2^n, and check the ones that satisfy the sum.
i''d make a matrix of 2^n x 2, where for each combination, we''ll have the combination and its sum, then check sum by sum if it matches with your number.
e.g.
list: 1, 2, 3
sum = 3
the matrix will be of 2^3 = 8 x 2 (actually 7x2, because we''ll not consider the empty one)
the matrix will look like his:
听起来像是对我的挑战!!
^。^
好吧,因为它已经晚了晚上,我不会制作最好的算法,但如果列表少于170个数字(因为双重不允许任何因子超过170,并且我使用组合),这个将工作
这是为Excel的VBA制作的,它将从A列读取列表(不应该有空格)并从单元格B1中读取总和。
如果重复列表的任何值,它将无法工作,并且它不会显示明显的解决方案(即不会显示只有一个数字的解决方案,只有2个或更多成员的组合列表)
Sounds like a challenge to me!!
^.^
Well, since its late at night, i wont make the best algorithm, but this one will work if the list has less than 170 numbers (because a double wont allow any factorial over 170, and im using combinations)
This is made for Excel''s VBA, it''ll read the list from column A (there shouldnt be blank spaces) and reads the sum from cell B1.
It wont work if any value of the list is repeated, and it wont show the obvious solution (i.e.it wont show the solutions of only one number, only combinations of 2 or more members of the list)
这篇关于在列表中查找总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!