可能的组合计算器

时间:2014-11-17 20:49:12

标签: arrays vb.net permutation

我想计算数字(0到9),大写字母(A到Z)和小写字母(a到z)中的所有可能组合。

数组一次只包含10位数字或字母。

我在vb.net中尝试这个但是我发现它有点令人困惑。

任何人都知道如何做到这一点?

2 个答案:

答案 0 :(得分:0)

每个26 * 2 + 10 = 62个字符的10个地方的组合数量将给出62 ^ 10个不同的字符串。如果你想将它们存储在一个记忆中..祝你好运。如果你想逐个处理它们......运气也好。但通常该方法是递归的(伪代码:

perms(n, chars)
   while (characters remaining in chars)
       firstChar = getNextCharacterFrom(chars)
       output <- firstChar + perms(n-1, chars)

答案 1 :(得分:0)

这是一种迭代方法,可以让你从任何位置开始序列。通过这种方式,您可以停止序列,存储当前值,然后稍后从相同位置重新启动它,即使您关闭了应用程序(显然您必须将该值存储在某个地方,如文本文件)。

这只是我的代码的修改版本: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23723548.html#a22452485

Sleep()调用只是为了让您可以看到标签中的值:

Public Class Form1

    Private Rev As Revision

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' You can start from anywhere in the sequence by passing in a different starting value to the second parameter below:
        Rev = New Revision("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "aaaaaaaaaa")
        Label1.Text = Rev.CurrentRevision
    End Sub

    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        System.Threading.Thread.Sleep(100)
        Dim curRevision As String = Rev.NextRevision
        While curRevision.Length = 10
            BackgroundWorker1.ReportProgress(-1, curRevision)
            System.Threading.Thread.Sleep(50)
            curRevision = Rev.NextRevision
        End While
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Label1.Text = e.UserState
    End Sub

End Class

Public Class Revision

    Private chars As String
    Private values() As Char
    Private curRevision As System.Text.StringBuilder

    Public Sub New()
        Me.DefaultRevision()
    End Sub

    Public Sub New(ByVal validChars As String)
        If validChars.Length > 0 Then
            chars = validChars
            values = chars.ToCharArray()
            curRevision = New System.Text.StringBuilder(values(0))
        Else
            Me.DefaultRevision()
        End If
    End Sub

    Public Sub New(ByVal validChars As String, ByVal startingRevision As String)
        Me.New(validChars)
        curRevision = New System.Text.StringBuilder(startingRevision)
        Dim i As Integer
        For i = 0 To curRevision.Length - 1
            If Array.IndexOf(values, curRevision.Chars(i)) = -1 Then
                curRevision = New System.Text.StringBuilder(values(0))
                MessageBox.Show("Revision has been reset." & vbCrLf & "Current Revision = " & Me.CurrentRevision, "Starting Revision contains an Invalid Character", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit For
            End If
        Next
    End Sub

    Private Sub DefaultRevision()
        chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        values = chars.ToCharArray
        curRevision = New System.Text.StringBuilder(values(0))
    End Sub

    Public ReadOnly Property ValidChars() As String
        Get
            Return chars
        End Get
    End Property

    Public ReadOnly Property CurrentRevision() As String
        Get
            Return curRevision.ToString()
        End Get
    End Property

    Public Function NextRevision(Optional ByVal numRevisions As Integer = 1) As String
        Dim forward As Boolean = (numRevisions > 0)
        numRevisions = Math.Abs(numRevisions)
        Dim i As Integer
        For i = 1 To numRevisions
            If forward Then
                Me.Increment()
            Else
                Me.Decrement()
            End If
        Next
        Return Me.CurrentRevision
    End Function

    Private Sub Increment()
        Dim curChar As Char = curRevision.Chars(curRevision.Length - 1)
        Dim index As Integer = Array.IndexOf(values, curChar)
        If index < (chars.Length - 1) Then
            index = index + 1
            curRevision.Chars(curRevision.Length - 1) = values(index)
        Else
            curRevision.Chars(curRevision.Length - 1) = values(0)
            Dim i As Integer
            Dim startPosition As Integer = curRevision.Length - 2
            For i = startPosition To 0 Step -1
                curChar = curRevision.Chars(i)
                index = Array.IndexOf(values, curChar)
                If index < (values.Length - 1) Then
                    index = index + 1
                    curRevision.Chars(i) = values(index)
                    Exit Sub
                Else
                    curRevision.Chars(i) = values(0)
                End If
            Next
            curRevision.Insert(0, values(0))
        End If
    End Sub

    Private Sub Decrement()
        Dim curChar As Char = curRevision.Chars(curRevision.Length - 1)
        Dim index As Integer = Array.IndexOf(values, curChar)
        If index > 0 Then
            index = index - 1
            curRevision.Chars(curRevision.Length - 1) = values(index)
        Else
            curRevision.Chars(curRevision.Length - 1) = values(values.Length - 1)
            Dim i As Integer
            Dim startPosition As Integer = curRevision.Length - 2
            For i = startPosition To 0 Step -1
                curChar = curRevision.Chars(i)
                index = Array.IndexOf(values, curChar)
                If index > 0 Then
                    index = index - 1
                    curRevision.Chars(i) = values(index)
                    Exit Sub
                Else
                    curRevision.Chars(i) = values(values.Length - 1)
                End If
            Next
            curRevision.Remove(0, 1)
            If curRevision.Length = 0 Then
                curRevision.Insert(0, values(0))
            End If
        End If
    End Sub

End Class