Vb.net 字符串组合
我想生成一个单词组合。例如,如果我有以下列表: {猫、狗、马、猿、鸡、鼠} 那么结果就是n(n-1)/2 猫狗马猿母鼠 (猫狗)(狗马)(马猿)(猿母鸡)(母鼠) (猫、狗、马、猿、马、猿、鸡)等 希望这是有意义的…我发现的一切都涉及排列Vb.net 字符串组合,vb.net,nested-loops,Vb.net,Nested Loops,我想生成一个单词组合。例如,如果我有以下列表: {猫、狗、马、猿、鸡、鼠} 那么结果就是n(n-1)/2 猫狗马猿母鼠 (猫狗)(狗马)(马猿)(猿母鸡)(母鼠) (猫、狗、马、猿、马、猿、鸡)等 希望这是有意义的…我发现的一切都涉及排列 我的名单有500长假设你的名单是arr={猫、狗、马、猿、鸡、鼠} 然后你可以做: for i = 0; i < arr.size; i++) for j = i; j < arr.size; j++) print i,j; 表示i=
我的名单有500长假设你的名单是arr={猫、狗、马、猿、鸡、鼠} 然后你可以做:
for i = 0; i < arr.size; i++)
for j = i; j < arr.size; j++)
print i,j;
表示i=0;i
这个想法基本上是——对于每个项目,将其与列表中的其他项目配对。但是,为了避免重复(例如,1,2和2,1),您不必每次都从头开始启动内部循环,而是从外部循环的当前索引开始。假设您的列表是arr={cat、dog、horse、ape、hen、mouse} 然后你可以做:
for i = 0; i < arr.size; i++)
for j = i; j < arr.size; j++)
print i,j;
表示i=0;i
这个想法基本上是——对于每个项目,将其与列表中的其他项目配对。但是,为了避免重复(例如1,2和2,1),您不必每次都从头开始启动内部循环,而是从外部循环的当前索引开始启动。试试这个!:
Public Sub test()
Dim myAnimals As String = "cat dog horse ape hen mouse"
Dim myAnimalCombinations As String() = BuildCombinations(myAnimals)
For Each combination As String In myAnimalCombinations
'Look on the Output Tab for the results!
Console.WriteLine("(" & combination & ")")
Next combination
End Sub
Public Function BuildCombinations(ByVal inputString As String) As String()
'Separate the sentence into useable words.
Dim wordsArray As String() = inputString.Split(" ".ToCharArray)
'A plase to store the results as we build them
Dim returnArray() As String = New String() {""}
'The 'combination level' that we're up to
Dim wordDistance As Integer = 1
'Go through all the combination levels...
For wordDistance = 1 To wordsArray.GetUpperBound(0)
'Go through all the words at this combination level...
For wordIndex As Integer = 0 To wordsArray.GetUpperBound(0) - wordDistance
'Get the first word of this combination level
Dim combination As New System.Text.StringBuilder(wordsArray(wordIndex))
'And all all the remaining words a this combination level
For combinationIndex As Integer = 1 To wordDistance
combination.Append(" " & wordsArray(wordIndex + combinationIndex))
Next combinationIndex
'Add this combination to the results
returnArray(returnArray.GetUpperBound(0)) = combination.ToString
'Add a new row to the results, ready for the next combination
ReDim Preserve returnArray(returnArray.GetUpperBound(0) + 1)
Next wordIndex
Next wordDistance
'Get rid of the last, blank row.
ReDim Preserve returnArray(returnArray.GetUpperBound(0) - 1)
'Return combinations to the calling method.
Return returnArray
End Function
第一个函数只是演示如何调用第二个函数。这实际上取决于你如何得到你的500个名单——你可以复制并粘贴在动物的名字上,或者你可以加载一个包含单词的文件。如果一条线不合适,您可以尝试:
Dim myAnimals As New StringBulder
myAnimals.Append("dog cat ... animal49 animal50")
myAnimals.Append(" ")
myAnimals.Append("animal51 ... animal99")
myAnimals.Append(" ")
myAnimals.Append("animal100 ... animal150")
等等
然后
试试这个!:
Public Sub test()
Dim myAnimals As String = "cat dog horse ape hen mouse"
Dim myAnimalCombinations As String() = BuildCombinations(myAnimals)
For Each combination As String In myAnimalCombinations
'Look on the Output Tab for the results!
Console.WriteLine("(" & combination & ")")
Next combination
End Sub
Public Function BuildCombinations(ByVal inputString As String) As String()
'Separate the sentence into useable words.
Dim wordsArray As String() = inputString.Split(" ".ToCharArray)
'A plase to store the results as we build them
Dim returnArray() As String = New String() {""}
'The 'combination level' that we're up to
Dim wordDistance As Integer = 1
'Go through all the combination levels...
For wordDistance = 1 To wordsArray.GetUpperBound(0)
'Go through all the words at this combination level...
For wordIndex As Integer = 0 To wordsArray.GetUpperBound(0) - wordDistance
'Get the first word of this combination level
Dim combination As New System.Text.StringBuilder(wordsArray(wordIndex))
'And all all the remaining words a this combination level
For combinationIndex As Integer = 1 To wordDistance
combination.Append(" " & wordsArray(wordIndex + combinationIndex))
Next combinationIndex
'Add this combination to the results
returnArray(returnArray.GetUpperBound(0)) = combination.ToString
'Add a new row to the results, ready for the next combination
ReDim Preserve returnArray(returnArray.GetUpperBound(0) + 1)
Next wordIndex
Next wordDistance
'Get rid of the last, blank row.
ReDim Preserve returnArray(returnArray.GetUpperBound(0) - 1)
'Return combinations to the calling method.
Return returnArray
End Function
第一个函数只是演示如何调用第二个函数。这实际上取决于你如何得到你的500个名单——你可以复制并粘贴在动物的名字上,或者你可以加载一个包含单词的文件。如果一条线不合适,您可以尝试:
Dim myAnimals As New StringBulder
myAnimals.Append("dog cat ... animal49 animal50")
myAnimals.Append(" ")
myAnimals.Append("animal51 ... animal99")
myAnimals.Append(" ")
myAnimals.Append("animal100 ... animal150")
等等
然后
他是否想要实现包含2^n-1个元素的列表的关闭?他是否想要实现包含2^n-1个元素的列表的关闭?快速提问…如果列表有500个单词长;如果列表长度为500个单词,我将如何包括或更改第一个函数;否则,我将如何包括或更改第一个函数。这只打印他不想做的所有组合对。这只打印他不想做的所有组合对。