1)首先生成一段由字母“a,b,c,A,B,C"随机组成的字符串s;
2)对字符串s从左到右寻找3个及以上连续相同字母(不区分大小写)组成的字符串并进行消除;
3)对消除后的重组字符串再进行相同消除操作,直至完成所有消除操作。
例如,生成字符串s为“BCbbaabAbAaaA",第1次消除字符串“aaa” 后,字符串s重组为“BCcbbbAbAaaA”;第2次消除字符串“bbb" 后,字符串s重组为“BCcAbAaaA”;第3次消除字符串“AaaA” 后,字符串s重组为“BCcAb”。
小陈编写了VB程序,功能如下:单击“生成字符串”按钮Command1, 随机生成由字母“a,b,c,A,B,C”组成的字符串s并显示在文本框Text1中,单击“消除”按钮Command2,在列表框List1中显示各次消除的字符串,在文本框Text2中显示消除后的字符串。程序运行界面如图所示。
(提示:字母“a”的ascii码值为97,字母“A”的asci码值为65)
Dim a(1 To 100) As String
Dim b(1 To 100) As String
Dim s As String
Constn= 15
Private Sub Command1_ Click0
Dim i As Integer
Randomize
For i= 1 To n
a(i)= Cr
s=s+ a(i)
Next i
Text1.Text=s
End Sub
Private Sub Command2 _Click)
Dim I As Integer, j As Integer, m As Integer, k As Integer
i=1
m= 1
Do Whilei <= Len(s) -2
k=i
Do While IsSame(a(i), a(i + 1)) Andi<= Len(s)- 1
i=i+ 1
If i= n Then Exit Do
Loop
If i-k>= 2 Then
b(m) =
List1 AddItem "第" + Str(m) + "次消除的字符: " + b(m)
m=m+ 1
s= Mid(s, 1, k- 1) + Mid(s, i+ 1, Len(S)-i)
For j= 1 To Len(s)
a(j)= Mid(s, j, 1)
Next j
i=k-3
If i<0 Then i=0
End If
i=i+1
Loop
Text2.Text=s
End Sub
Function IsSame(p As String, q As String) As Boolean
IsSame = False
If Then
IsSame = True
End If
End Function