这个序列生成问题的名称是什么?有什么意见吗?
我需要迭代由数字数组定义的有序序列 ai, i = 1 ..n,其中n是每个序列元素的长度,每个ai< /sub> 指定输出序列中位置 i 处可能值的最大数量。
示例:
a = {10,10,10}
序列:000
、001
、002
、...999
(000< /code> 到
999
)a = (2,3,2}
序列:000
、001
、010
、011
、020
、021、
100
、101
、110
、111
、120
,121
(注意:我不仅需要打印序列,还需要迭代其元素,其中每个元素都是一个数组,例如 {1,2,1}。
)我实现了这个,想问一下大家有什么意见吗?也许这是一个已知问题(名称?),并且已经有可用的代码,或者它简化为其他一些众所周知的问题?它确实与排列问题有相似之处。
I need to iterate over an ordered sequence that is defined by an array of numbers ai, i = 1..n, where n is the length of each sequence element, and each ai specifies the max number of possible values at position i in the output sequence.
Example:
a = {10,10,10}
Sequence:000
,001
,002
, ...999
(the decimal numbers from000
to999
)a = (2,3,2}
Sequence:000
,001
,010
,011
,020
,021
,100
,101
,110
,111
,120
,121
(Note: I don't just need to print the sequence, but I need to iterate over its elements, where each element is an array, e.g. {1,2,1}.)
Before I implement this, I wanted to ask if anyone has any comments? Maybe this is a known problem (name?), and there is already code available, or it reduces to some other well-known problem? It does have similarities to the permutation problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个笛卡尔积。
在Python中,
itertools.product
产生这样的元组列表。在其他语言中,使用嵌套循环创建笛卡尔积很容易:
This is a Cartesian product.
In Python,
itertools.product
produces such lists of tuples.In other languages, it's easy to create a Cartesian product using nested loops:
序列的元素按字典顺序列出。另请参阅此(另一个不同但相关的问题)。
The elements of the sequences are listed in the lexographic order. Also see this (a different but related problem).
如果您使用 C# (linq) 并且笛卡尔积中的因子数是静态的,那么它也可以非常优雅地表达
If you are using C# (linq) and the number of factors in the cartesian product is static, then it can also be quite elgantly expressed