16,554
社区成员
发帖
与我相关
我的任务
分享
Sub Main()
PrintString(String.Empty, 0, 10)
Console.ReadKey()
End Sub
Sub PrintString(str As String, position As Integer, length As Integer)
Dim wz() As String = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
For i As Integer = 0 To UBound(wz)
Dim result = str + wz(i)
If position = length - 1 Or position = UBound(wz) Then
Console.WriteLine(result)
Else
PrintString(result, position + 1, length)
End If
Next
End Sub
不用递归,写十层for next其实也行Dim wz() As String = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
Dim cnt As Integer
For Each r In 组合(wz, 10)
......................
Module Module1
Sub Main()
Dim wz() As String = {"0", "1", "2", "a", "b", "c"}
Dim cnt As Integer
For Each r In 组合(wz, 3)
Dim x = String.Join(String.Empty, r.ToArray)
cnt += 1
Console.WriteLine("{0}:{1}", cnt, x)
Next
Console.WriteLine(".....按任意键结束")
Console.ReadKey()
End Sub
Public Iterator Function 组合(wz As String(), count As Integer) As IEnumerable(Of List(Of String))
If (count <= 0) Then
Yield New List(Of String)
Else
For Each k In 组合(wz, count - 1)
For Each w In wz
Dim u = k.ToList
u.Add(w)
Debug.Assert(u.Count = count)
Yield u
Next
Next
End If
End Function
End Module
你可以看到,6个字符,会得到 216 个结果。