得到所有可能的组合,从号码列表组合、号码、列表

由网友(堇色安年)分享简介:我在寻找实现这一目标的有效途径:I'm looking for an efficient way to achieve this:你有数字1的列表..... N(通常为:1..5或1..7左右 - 相当小,但可以从情况而发生变化)you have a list of numbers 1.....n (typicall...

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

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.

阅读全文

相关推荐

最新文章