今天小弟我朋友的面试题…高手帮忙
今天我朋友的面试题……高手帮忙。
有a b c d四个字符,用递归的方法将其能出现的组合打印出来。
如:
a b c d
a b d c
a c b d
……
------解决方案--------------------
Option Explicit
Dim AData(4) As Integer
Private Sub PrintResult()
Dim iLoop As Integer
For iLoop = 0 To 3
Debug.Print Chr(AData(iLoop) + Asc( "a ")) & " ";
Next iLoop
Debug.Print
End Sub
Private Sub Try(ByVal iLevel As Integer)
Dim iLoop As Integer
Dim iFind As Integer
Dim bExist As Boolean
If iLevel > = 4 Then
PrintResult
Else
iLoop = 0
Do While iLoop <= 3
bExist = False
For iFind = 0 To iLevel - 1
If AData(iFind) = iLoop Then
bExist = True
Exit For
End If
Next iFind
If Not bExist Then
AData(iLevel) = iLoop
Try iLevel + 1
AData(iLevel) = -1
End If
iLoop = iLoop + 1
Loop
End If
End Sub
Private Sub Command1_Click()
Dim iLoop As Integer
For iLoop = 0 To 3
AData(iLoop) = -1
Next iLoop
Try (0)
End Sub
有a b c d四个字符,用递归的方法将其能出现的组合打印出来。
如:
a b c d
a b d c
a c b d
……
------解决方案--------------------
Option Explicit
Dim AData(4) As Integer
Private Sub PrintResult()
Dim iLoop As Integer
For iLoop = 0 To 3
Debug.Print Chr(AData(iLoop) + Asc( "a ")) & " ";
Next iLoop
Debug.Print
End Sub
Private Sub Try(ByVal iLevel As Integer)
Dim iLoop As Integer
Dim iFind As Integer
Dim bExist As Boolean
If iLevel > = 4 Then
PrintResult
Else
iLoop = 0
Do While iLoop <= 3
bExist = False
For iFind = 0 To iLevel - 1
If AData(iFind) = iLoop Then
bExist = True
Exit For
End If
Next iFind
If Not bExist Then
AData(iLevel) = iLoop
Try iLevel + 1
AData(iLevel) = -1
End If
iLoop = iLoop + 1
Loop
End If
End Sub
Private Sub Command1_Click()
Dim iLoop As Integer
For iLoop = 0 To 3
AData(iLoop) = -1
Next iLoop
Try (0)
End Sub