Vb.net所有组合 [英] Vb.net all combinations

查看:107
本文介绍了Vb.net所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有6个项目需要组合,



列出的项目:(这是一个示例)





  • 母牛

  • 鹿|小鹿|大鹿

  • 绵羊



所以鹿有2个子项。



这些物品都列在这样的列表中:





{鹿|小鹿|大鹿}
绵羊



所以您可以看到哪个项目有更多项目。



我做什么所有这些组合都需要:






  • 鹿
    绵羊




  • 小鹿




  • 大鹿
    绵羊




(有时6个项目,有时更少。



有人可以帮助我吗?



编辑:



有时列表也是这样的:






  • 鹿|小鹿|大鹿

  • 绵羊

  • 鼠标|黑色鼠标|白色鼠标



(所以有更多带有子项的项目)

解决方案

这是VB 2010的递归解决方案(当组合很多时,性能可能不是很好,但这只是一个开始):

 函数GetCombinations(项为String()(),可选索引为Integer = 0)作为List(Of String())
暗淡组合为新List(Of String())
Dim lastIndex = items.Count-1
选择个案索引
个案< 0,是> lastIndex
抛出New ArgumentException( index应该为0或更大)
case lastIndex
每个项目in items(index)
groups.Add({item})
下一个
其他情况
昏暗的nextCombinations = GetCombinations(物品,索引+ 1)
每个项目中的项目(索引)
每个每个nextCombination在nextCombinations
个组合.Add({item} .Concat(nextCombination).ToArray)
下一个
下一个
结束选择
返回组合
结束函数

测试代码:

 暗物品= {({ Ape}),({ Cow}),({ Deer, Small鹿, Big deer}),({ Sheep}),({ Mouse,  Black Mouse, White mouse}}} 
对于GetCombinations(items)中的每个组合
Console.WriteLine(String.Join(,,组合))
下一个

测试ou tputs:

 猿,牛,鹿,绵羊,老鼠
猿,牛,鹿,绵羊,黑老鼠
猿,牛,鹿,绵羊,白老鼠
猿,牛,小鹿,绵羊,老鼠
猿,牛,小鹿,绵羊,黑老鼠
猿,牛,小鹿,绵羊,白老鼠
猿,牛,大鹿,绵羊,老鼠
猿,牛,大鹿,绵羊,黑老鼠
猿,牛,大鹿,绵羊,白色鼠标


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 sheep

  • ape cow small deer sheep

  • ape 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 )

解决方案

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):

Function GetCombinations(items As String()(), Optional index As Integer = 0) As List(Of String())
    Dim combinations As New List(Of String())
    Dim lastIndex = items.Count - 1
    Select Case index
        Case Is < 0, Is > lastIndex
            Throw New ArgumentException("index should be 0 or greater")
        Case lastIndex
            For Each item In items(index)
                combinations.Add({item})
            Next
        Case Else
            Dim nextCombinations = GetCombinations(items, index + 1)
            For Each item In items(index)
                For Each nextCombination In nextCombinations
                    combinations.Add({item}.Concat(nextCombination).ToArray)
                Next
            Next
    End Select
    Return combinations
End Function

Test code:

Dim items = {({"Ape"}), ({"Cow"}), ({"Deer", "Small deer", "Big deer"}), ({"Sheep"}), ({"Mouse", "Black Mouse", "White mouse"})}
For Each combination In GetCombinations(items)
    Console.WriteLine(String.Join(", ", combination))
Next

Test outputs:

Ape, Cow, Deer, Sheep, Mouse
Ape, Cow, Deer, Sheep, Black Mouse
Ape, Cow, Deer, Sheep, White mouse
Ape, Cow, Small deer, Sheep, Mouse
Ape, Cow, Small deer, Sheep, Black Mouse
Ape, Cow, Small deer, Sheep, White mouse
Ape, Cow, Big deer, Sheep, Mouse
Ape, Cow, Big deer, Sheep, Black Mouse
Ape, Cow, Big deer, Sheep, White mouse

这篇关于Vb.net所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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