得到所有可能的组合,从号码列表 [英] Getting all possible combinations from a list of numbers

查看:140
本文介绍了得到所有可能的组合,从号码列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找实现这一目标的有效途径:

I'm looking for an efficient way to achieve this:

  • 你有数字1的列表..... N(通常为:1..5或1..7左右 - 相当小,但可以从情况而发生变化)

  • you have a list of numbers 1.....n (typically: 1..5 or 1..7 or so - reasonably small, but can vary from case to case)

您需要的所有长度的所有组合对于这些数字,如:的一个号码的所有组合({1},{2},.... {n}的),那么两个不同的数字({1,2},{1,3},{1,4}的所有组合。 ... {N-1,N}),然后FO其中三个数字({1,2,3}所有组合,{1,2,4})等

you need all combinations of all lengths for those numbers, e.g. all combinations of just one number ({1}, {2}, .... {n}), then all combinations of two distinct numbers ({1,2}, {1,3}, {1,4} ..... {n-1, n} ), then all combinations fo three of those numbers ({1,2,3}, {1,2,4}) and so forth

基本上,在集团内部,顺序是不相关的,所以{1,2,3}等价于{1,3,2} - 这让所有组×号从该列表只是一个问题。

Basically, within the group, the order is irrelevant, so {1,2,3} is equivalent to {1,3,2} - it's just a matter of getting all groups of x numbers from that list

好像应该有一个简单的算法,这一点 - 但我已经搜查徒劳至今。大多数组合数学和置换算法似乎一)取顺序考虑(如123不等于132),他们似乎总是在字符或数字的单串......

Seems like there ought to be a simple algorithm for this - but I have searched in vain so far. Most combinatorics and permutation algorithms seems to a) take order into account (e.g. 123 is not equal to 132), and they always seems to operate on a single string of characters or numbers....

任何人都有一个伟大的,nice'n'quick算法他们的袖子?

Anyone have a great, nice'n'quick algorithm up their sleeve??

谢谢!

推荐答案

只是增加一个二进制数,并采取相应的设置位的元素。

Just increment a binary number and take the elements corresponding to bits that are set.

例如, 00101101 将意味着需要在索引0,2,3 5的元素,由于您的列表只是1..N,元素很简单该指数+ 1。

For instance, 00101101 would mean take the elements at indexes 0, 2, 3, and 5. Since your list is simply 1..n, the element is simply the index + 1.

这将产生有序permuatations。换句话说,只有 {1,2,3} 将生成。不 {1,3,2} {2,1,3} { 2,3,1} 等。

This will generate in-order permuatations. In other words, only {1, 2, 3} will be generated. Not {1, 3, 2} or {2, 1, 3} or {2, 3, 1}, etc.

这篇关于得到所有可能的组合,从号码列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆