编写“构造字符串”程序,实现如下功能:输入不重复的N个字符(2≤N≤5),构造由该N个字符(允许重复)组成的N位的字符串,输出所构造的字符串及构造的方法种数。程序运行界面如图所示。
实现上述功能的VB代码如下:
Private Sub Command1_Click() Dim a(0 To 4) As String Dim b(0 To 4) As Integer
Dim s As String, r As String
Dim i As Integer, j As Integer, k As Integer, c As Integer, n As Integer
List1.Clear
s = Text1.Text : n = Len(s)
If Then '选填 Label1.Caption = "数据输入有误!"
Exit Sub '退出 End If
For i = 0 To n - 1
a(i) = Mid(s, i + 1, 1) b(i) = 0
Next i c = 0
For i = 1 To n ^ n r = ""
For j = 0 To n - 1
Next j
c = c + 1 List1.AddItem r k = 0
b(k) = b(k) + 1
Do While b(k) = n And k < 4 b(k) = 0
b(k) = b(k) + 1 Loop
Next i
Label1.Caption = "构造字符串的方法共计" + Str(c) + "种。" End Sub
Function test(s As String) As Boolean ‘用于判别是否存在重复的字符,代码略
‘若无重复,则返回True,否则返回False
End Function
请回答下列问题: