Vb.net所有组合
我有 6 个项目需要组合,
列出的项目:(这是一个例子)
- 猿
- 猿牛
- 牛鹿 | 鹿小鹿|大鹿
- 羊,
所以“鹿”有 2 个子项。
这些项目都在如下列出的列表中:
ape 奶牛 {鹿|小鹿|大鹿} 羊,
这样你就可以看到哪个项目有更多的项目。
我想要的是所有这些组合:
ape 奶牛 鹿
猿 奶牛 小鹿
猿 奶牛 大鹿
(有时有超过 6 个项目,有时更少。
有人可以帮助我吗?
编辑:
有时列表也是这样的:
- 猿
- 牛
- 鹿 |小鹿|大鹿
- 羊
- 鼠|黑鼠|白鼠
(因此更多带有子项目的项目)
I have 6 items which have to be combinated,
items listed: ( this is an example)
- Ape
- Cow
- Deer | Small deer | Big deer
- sheep
so 'deer' has 2 subitems.
these items are all in an list listed like this:
ape
cow
{deer | small deer | big deer}
sheep
so you can see which item has more items.
what i want is all those combinations:
ape
cow
deer
sheepape
cow
small deer
sheepape
cow
big deer
sheep
( sometimes there are more then 6 items, sometimes there are less.
Is someone who can help me with this?
EDIT:
Sometimes the list is also something like this:
- ape
- cow
- Deer | Small deer | Big deer
- sheep
- mouse | Black mouse | White mouse
( so more items with subitems )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您将需要我的解决方案的变体,我在 在此解决方案中有类似的问题。也许这足以让你们相处?
可能你看不到答案,所以我把它复制到这里。有人想要将 1 和 2 的所有组合串联起来,长度为 3
您将需要放置按 1、2 等索引的数组值,而不是数字本身,并为每个子列表发明类似的操作。
You will need a variant of my solution, that I presented in HERE in this solution for a similar problem. maybe it suffices to get you along?
Perhaps you're not allowed to see the answer, so I copy it here. Someone wanted all combinations of 1 and 2 in series with length 3
You will need to put arrayvalues indexed by 1, 2, etc instead of the numbers itself, and invent a similar operation for each sublist.
我无法评论@Martin 提供的方法,因为我不是专家交换的成员,但以下是我解决该问题的方法。
您想要一个 IEnumerable (of IEnumerable (of T))
当您有“Ape”时,您需要将其视为 {Ape}。这就是你的清单:
{Ape},
{奶牛},
{鹿,小鹿,大鹿},
{sheep}
从那里,您可以通过对外部列表进行分组并迭代内部列表来构建组合。
IE 第一项 {Ape} 只有一个元素,因此您只需迭代一次。与 {Cow} 相同,但包含所有鹿的第三个将被迭代 3 次。
这应该足以让您开始。
I can't comment on the approach @Martin supplied as I'm not a member of Experts Exchange, but here is how I would approach the problem.
You want an IEnumerable (of IEnumerable (of T))
When you have "Ape", you need to think of it as {Ape}. That makes your list:
{Ape},
{Cow},
{Deer, Small deer, Big deer},
{sheep}
From there, you build your combinations by grouping the outer list and iterating through the inner list(s).
I.E. The first item {Ape} only has one element, so you would only iterate on that once. The same with {Cow}, but the third with all the deer would be iterated over 3 times.
That should be enough to get you started.
这是 VB 2010 的递归解决方案(当有很多组合时,性能可能不是很好,但它是一个开始):
测试代码:
测试输出:
Here's a recursive solution for VB 2010 (probably not very good performance-wise when there are a lot of combinations, but it's a start):
Test code:
Test outputs:
想要输入字符串吗?并且没有递归?那么这是最终的解决方案,是我之前示例的简单改编:
Want the input as strings? and no recursion? then here's the final solution, a simple adaptation of my previous sample: