Vb.net所有组合

发布于 2024-12-11 12:33:41 字数 518 浏览 0 评论 0原文

我有 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
    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 )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

娜些时光,永不杰束 2024-12-18 12:33:41

您将需要我的解决方案的变体,我在 在此解决方案中有类似的问题。也许这足以让你们相处?

可能你看不到答案,所以我把它复制到这里。有人想要将 1 和 2 的所有组合串联起来,长度为 3

   Dim HighestValue As Integer = 2 ' max value
    Dim NrOfValues As Integer = 3 ' nr of values in one result
    Dim Values(NrOfValues) As Integer
    Dim i As Integer
    For i = 0 To NrOfValues - 1
        Values(i) = 1
    Next
    Values(NrOfValues - 1) = 0 ' to generate first as ALL 1
    For i = 1 To HighestValue ^ NrOfValues
        Values(NrOfValues - 1) += 1
        For j As Integer = NrOfValues - 1 To 0 Step -1
            If Values(j) > HighestValue Then
                Values(j) = 1
                Values(j - 1) += 1
            End If
        Next
        Dim Result As String = ""
        For j As Integer = 0 To NrOfValues - 1
            Result = Result & CStr(Values(j))
        Next
        Debug.WriteLine(Result)
    Next

您将需要放置按 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

   Dim HighestValue As Integer = 2 ' max value
    Dim NrOfValues As Integer = 3 ' nr of values in one result
    Dim Values(NrOfValues) As Integer
    Dim i As Integer
    For i = 0 To NrOfValues - 1
        Values(i) = 1
    Next
    Values(NrOfValues - 1) = 0 ' to generate first as ALL 1
    For i = 1 To HighestValue ^ NrOfValues
        Values(NrOfValues - 1) += 1
        For j As Integer = NrOfValues - 1 To 0 Step -1
            If Values(j) > HighestValue Then
                Values(j) = 1
                Values(j - 1) += 1
            End If
        Next
        Dim Result As String = ""
        For j As Integer = 0 To NrOfValues - 1
            Result = Result & CStr(Values(j))
        Next
        Debug.WriteLine(Result)
    Next

You will need to put arrayvalues indexed by 1, 2, etc instead of the numbers itself, and invent a similar operation for each sublist.

随心而道 2024-12-18 12:33:41

我无法评论@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.

遥远的绿洲 2024-12-18 12:33:41

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

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

测试代码:

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

测试输出:

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

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
小瓶盖 2024-12-18 12:33:41

想要输入字符串吗?并且没有递归?那么这是最终的解决方案,是我之前示例的简单改编:

    Dim Lines As New List(Of List(Of String))
    AddItem(Lines, "a ")
    AddItem(Lines, "b ")
    AddItem(Lines, "c1|c2|c3 ")
    AddItem(Lines, "d ")
    AddItem(Lines, "e1|e2")
    AddItem(Lines, "f ") ' etc
    Dim i As Integer
    Dim j As Integer
    Dim ItemnrInLine(Lines.Count - 1) As Integer
    Dim NrCombinations = 1
    For i = 0 To (Lines.Count - 1)
        ItemnrInLine(i) = 0
        NrCombinations *= Lines(i).Count
    Next
    ItemnrInLine(Lines.Count - 1) = -1 ' to get first combination as solution
    For i = 1 To NrCombinations
        ItemnrInLine(Lines.Count - 1) += 1
        For j = Lines.Count - 1 To 0 Step -1
            If ItemnrInLine(j) = Lines(j).Count Then
                ItemnrInLine(j) = 0
                ItemnrInLine(j - 1) += 1
            End If
        Next
        printOut(Lines, ItemnrInLine)
    Next

Sub printOut(ByVal Lines As List(Of List(Of String)), ByVal ItemnrInLine() As Integer)
    Dim Result As String = ""
    For k = 0 To Lines.Count - 1
        Result = Result & Lines(k)(ItemnrInLine(k)).Trim & " "
    Next
    Debug.WriteLine(Result)
End Sub

Sub AddItem(ByVal Lines As List(Of List(Of String)), ByVal inputString As String)
    Dim words() As String = inputString.Split("|"c)
    Dim wordList As New List(Of String)
    For i As Integer = 0 To words.Count - 1
        wordList.Add(words(i))
    Next
    Lines.Add(wordList)
End Sub

Want the input as strings? and no recursion? then here's the final solution, a simple adaptation of my previous sample:

    Dim Lines As New List(Of List(Of String))
    AddItem(Lines, "a ")
    AddItem(Lines, "b ")
    AddItem(Lines, "c1|c2|c3 ")
    AddItem(Lines, "d ")
    AddItem(Lines, "e1|e2")
    AddItem(Lines, "f ") ' etc
    Dim i As Integer
    Dim j As Integer
    Dim ItemnrInLine(Lines.Count - 1) As Integer
    Dim NrCombinations = 1
    For i = 0 To (Lines.Count - 1)
        ItemnrInLine(i) = 0
        NrCombinations *= Lines(i).Count
    Next
    ItemnrInLine(Lines.Count - 1) = -1 ' to get first combination as solution
    For i = 1 To NrCombinations
        ItemnrInLine(Lines.Count - 1) += 1
        For j = Lines.Count - 1 To 0 Step -1
            If ItemnrInLine(j) = Lines(j).Count Then
                ItemnrInLine(j) = 0
                ItemnrInLine(j - 1) += 1
            End If
        Next
        printOut(Lines, ItemnrInLine)
    Next

Sub printOut(ByVal Lines As List(Of List(Of String)), ByVal ItemnrInLine() As Integer)
    Dim Result As String = ""
    For k = 0 To Lines.Count - 1
        Result = Result & Lines(k)(ItemnrInLine(k)).Trim & " "
    Next
    Debug.WriteLine(Result)
End Sub

Sub AddItem(ByVal Lines As List(Of List(Of String)), ByVal inputString As String)
    Dim words() As String = inputString.Split("|"c)
    Dim wordList As New List(Of String)
    For i As Integer = 0 To words.Count - 1
        wordList.Add(words(i))
    Next
    Lines.Add(wordList)
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文