在n个连续的方格内填字母A或B,但相邻两格都不能填B,求所有可能方案。可以用枚举算法解决,此题可以采用进制转换来解决。考虑到只包含了A与B两个字母,我们可以用二进制的0和1来代替。
程序部分代码如下:
Private Sub Command1_Click()
Dim n, m, s, i, j, count
As Long
Dim flag As Boolean
Dim a(1 To 100) As Integer
n = Val(Text1.Text)
count = 0
①
For i = 0 to s-1
m = i
For j = 1 To n
a(j) = m Mod 2
m = m \ 2
Next j
flag = True
For j = 1 To n - 1
If ② Then
flag = false: exit For
Next j
If flag = True Then ③
Next i
Label1.Caption =“可行方案共”+ Str(count) + “种”
End Sub