根据位置计算组合
我有这样的组合:
1,2,3,4 //index 0
1,2,3,5 //索引 1
1,2,3,6 //索引 2
依此类推,直到 7,8,9,10
所以这将是组合数学中的 n=10 k=4
如何通过索引计算组合
例如当我的索引==1
myCmb = func(索引)
返回 1,2,3,5
这是一个例子,我需要这个来获得最大的数字,并且需要更多的参数,并且没有(如果可能的话)很多循环,
我找到这样的东西来获取位置: http://answers.yahoo.com/question/index?qid=20110320070039AA045ib
我现在想扭转这个......
我用 C++ 编程 感谢您的任何建议或帮助
I have combinations like this:
1,2,3,4 //index 0
1,2,3,5 //index 1
1,2,3,6 //index 2
and so on until 7,8,9,10
So this will be n=10 k=4 from combinatorics
How calculate combination by index
For example when my index==1
myCmb = func(index)
returns 1,2,3,5
this is example i need this for bigest numbers, and for more params and without (if this possible) many loops
i find something like this to obtain position:
http://answers.yahoo.com/question/index?qid=20110320070039AA045ib
I want now reverse this...
I programming in C++
Thanks for any sugesstions or help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎您想要查找给定数字的 k 组合。
按照示例,以下是应该有效的内容:
请注意,为了方便起见,该代码依赖于 Boost 来计算二项式系数(
boost::math::binomial_coefficient
),并将字符串解析为整数 (boost::lexical_cast
)。It seems like you want to find the k-combination for a given number.
Following the example, here's something that should work:
Note, for convenience, the code depends on Boost to calculate binomial coefficients (
boost::math::binomial_coefficient<T>
), and to parse strings into integers (boost::lexical_cast
).下面是 Mathematica 中的一个实现,来自 Combinatorica 包。语义相当通用,所以我认为它很有帮助。如果您需要任何解释,请发表评论。
用法如下:
正如您所看到的,该函数在集合上运行。
下面是我尝试解释上面的代码。
UnrankKSubset 是一个具有三个参数的递归函数,称为 (m, k, s):
k
一个整数,每个组合中的元素数量s
一个列表,从中组装组合的元素递归有两个边界条件:
对于任何排名
m
,以及任何列表s
,如果每个组合k
中的元素数量为1
,则:返回列表
s
的m + 1
元素,它本身在列表中。(
+ 1
是必需的,因为 Mathematica 从 1 开始索引,而不是从零开始。我相信在 C++ 中这将是 s[m] )如果排名
m
是0
那么对于任何k
和任何s
:返回
s
的前
k
个元素主递归函数,对于除上面指定的:
局部变量:(
i
,n
,x1
,u
)二项式 是二项式系数:
Binomial[7, 5]
= 21执行:
然后返回:
即“前置”列表
sx1
元素code> (记住 Mathematica 索引从 1 开始,所以我相信这将是 C++ 中的x1 - 1
索引)到递归调用 UnrankKSubset 函数返回的列表,参数为:m - u + Binomial[i, k]
k - 1
Drop[s, x1]
Drop[s, x1]
是其余的列表s
中删除了第一个x1
元素。如果以上任何内容无法理解,或者如果您想要的是算法的解释,而不是代码的解释,请发表评论,我会再试一次。
Here is an implementation in Mathematica, from the package Combinatorica. The semantics are fairly generic, so I think it is helpful. Please leave a comment if you need anything explained.
Usage is like:
As you can see this function operates on sets.
Below is my attempt to explain the code above.
UnrankKSubset is a recursive function with three arguments, called (m, k, s):
m
an Integer, the "rank" of the combination in lexigraphical order, starting from zero.k
an Integer, the number of elements in each combinations
a List, the elements from which to assemble combinationsThere are two boundary conditions on the recursion:
for any rank
m
, and any lists
, if the number of elements in each combinationk
is1
, then:return the
m + 1
element of the lists
, itself in a list.(
+ 1
is needed because Mathematica indexes from one, rather than zero. I believe in C++ this would be s[m] )if rank
m
is0
then for anyk
and anys
:return the first
k
elements ofs
The main recursive function, for any other arguments than ones specified above:
local variables: (
i
,n
,x1
,u
)Binomial
is binomial coefficient:Binomial[7, 5]
= 21Do:
Then return:
That is, "prepend" the
x1
element of lists
(remember Mathematica indexes from one, so I believe this would be thex1 - 1
index in C++) to the list returned by the recursively called UnrankKSubset function with the arguments:m - u + Binomial[i, k]
k - 1
Drop[s, x1]
Drop[s, x1]
is the rest of lists
with the firstx1
elements removed.If anything above is not understandable, or if what you wanted was an explanation of the algorithm, rather than an explanation of the code, please leave a comment and I will try again.