2011-10-21 80 views
1

我有6項,其必須被combinated,列出Vb.net所有組合

項目:(這是一個例子)

  • 鹿|小鹿|大鹿

所以 '鹿' 有2子項。

這些項目都是在這樣的上市名單:

猿 牛 {鹿|小鹿|大鹿} 綿羊

所以你可以看到哪個項目有更多的項目。

我想是所有這些組合:

  • 猿 牛 鹿 羊

  • 猿 牛 小鹿 羊

  • 猿 牛 大鹿 綿羊

(有時候會有更多的6個項目,有時候會少些。

有人可以幫助我嗎?

編輯:

有時名單也是這樣的:

  • 鹿|小鹿|大鹿
  • 綿羊
  • mouse |黑老鼠|白老鼠

(使更多的項目與子項目)

+0

在這最後一種情況下,你想要9行輸出? – Martin

回答

0

下面是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 
+0

非常感謝你 – Ruben

+0

我還有1個問題。我得到了一個字符串列表,包括我在開始時說的項目,我如何將它們轉換爲dim items =(這裏是所有這些項目) – Ruben

+0

對於列表中的每個字符串,請使用字符「|」作爲分隔符,並使用ToArray方法將結果轉換爲數組。 –

1

你需要我的解決方案的一個變體,我在HERE in this solution提出了類似的問題。也許它足以讓你一起?

也許你不允許看到答案,所以我把它複製到這裏。有人想要的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,等等,而不是數字本身索引arrayvalues,併發明瞭類似的操作對於每個子列表。

1

我不能評論@Martin提供的方法,因爲我不是專家交換所的成員,但這裏是我如何處理這個問題。

你想一個IEnumerable

(IEnumerable的(的T)的),當你有 「猿」,你需要把它看成{}猿。這使得您的列表:

{猿}, {牛}, {鹿,小鹿,大鹿}, {羊}

從那裏,你通過分組外列表構建組合迭代內部列表。

I.E.第一項{Ape}只有一個元素,所以你只能迭代一次。與{Cow}相同,但是所有鹿的第三個將迭代3次以上。

這應該足以讓你開始。

0

要輸入字符串嗎?並沒有遞歸?那麼這裏是最後的解決方案,我之前的樣本的一個簡單的改編:

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