--- Private Function Permutations(ByVal data As String) As String(,) Dim i As Int32 Dim y As Int32 Dim x As Int32 Dim tempChar As String Dim newString As String Dim strings(,) As String Dim rowCount As Long
If data.Length < 2 Then Exit Function End If
'use the factorial function to determine the number of rows needed 'because redim preserve is slow ReDim strings(data.Length - 1, Factorial(data.Length - 1) - 1) strings(0, 0) = data
'swap each character(I) from the second postion to the second to last position For i = 1 To (data.Length - 2) 'for each of the already created numbers For y = 0 To rowCount 'do swaps for the character(I) with each of the characters to the right For x = data.Length To i + 2 Step -1 tempChar = strings(0, y).Substring(i, 1) newString = strings(0, y) Mid(newString, i + 1, 1) = newString.Substring(x - 1, 1) Mid(newString, x, 1) = tempChar rowCount = rowCount + 1 strings(0, rowCount) = newString Next Next Next
'Shift Characters 'for each empty column For i = 1 To data.Length - 1 'move the shift character over one For x = 0 To strings.GetUpperBound(1) strings(i, x) = strings(i - 1, x) Mid(strings(i, x), i, 1) = strings(i - 1, x).Substring(i, 1) Mid(strings(i, x), i + 1, 1) = strings(i - 1, x).Substring(i - 1, 1) Next Next
Return strings
End Function
Public Function Factorial(ByVal Number As Integer) As String Try If Number = 0 Then Return 1 Else Return Number * Factorial(Number - 1) End If Catch ex As Exception Return ex.Message End Try End Function
Private Shared Sub FillAndCheck(ByVal s As String, ByVal ix As Integer, ByVal actlen As Integer, ByVal charset As Char(), ByVal used As Boolean(), ByVal target As String) If ix = actlen Then If s = target Then Console.WriteLine(target + " found") End If Else Dim tmp As String Dim i As Integer For i = 0 To charset.Length-1 If Not used(i) Then used(i) = True tmp = s + charset(i) FillAndCheck(tmp, ix + 1, actlen, charset, used, target) used(i) = False End If Next End If End Sub
Private Shared Sub FindOneLength(ByVal target As String, ByVal actlen As Integer, ByVal charset As String) Dim used(charset.Length - 1) As Boolean Dim j As Integer For j = 0 To used.length-1 used(j) = False Next FillAndCheck("", 0, actlen, charset.ToCharArray, used, target) End Sub
Public Shared Sub Find(ByVal target As String, ByVal maxlength As Integer, ByVal charset As String) Dim i As Integer For i = 1 To maxlength FindOneLength(target, i, charset) Next End Sub
Public Shared Sub Main(ByVal args As String()) Find("bc", 3, "abcd") Find("xy", 3, "abcd") Find("d", 3, "abcd") Find("dcba", 3, "abcd") End Sub End Class
Altså Console.WriteLine er jo nem at erstatte med sw.WriteLine - hvad skal der laves om i selve programmet ?
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.