算法总结数字的所有组合的列表 [英] algorithm to sum up a list of numbers for all combinations
本文介绍了算法总结数字的所有组合的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数字列表,我想添加了所有的不同组合。 例如:
I have a list of numbers and I want to add up all the different combinations. For example:
- 号1,4,7和13
- 的输出是:
1+4=5
1+7=8
1+13=14
4+7=11
4+13=17
7+13=20
1+4+7=12
1+4+13=18
1+7+13=21
4+7+13=24
1+4+7+13=25
有一个公式来计算这与不同的数字?
Is there a formula to calculate this with different numbers?
推荐答案
一个简单的方法是创建一个位设置尽可能多的位,因为有数字。 在您的例子4.
A simple way to do this is to create a bit set with as much bits as there are numbers. In your example 4.
再算上从0001到1111,总结每个都有1所设定的号码:
Then count from 0001 to 1111 and sum each number that has a 1 on the set:
数字1,4,7,13:
Numbers 1,4,7,13:
0001 = 13=13
0010 = 7=7
0011 = 7+13 = 20
1111 = 1+4+7+13 = 25
这篇关于算法总结数字的所有组合的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文