按上述要求,编写VB程序,功能如下:在文本框Text1中输入n的值,点击“开始枚举”按钮Command1,在列表框List1中输出所有方案,在列表框List2中输出比例合适方案。
实现上述功能的VB程序如下,回答下列问题:
Dim n As Integer, Getstr As String
Private Sub Command1_Click()
Dim i As Integer, j As Integer, mAs Integer
n = Val(Text1.Text)
Getstr = ""
For i = 0 To 2 ^ n -1
m = i
If Then
For j = 1 To n
If m Mod 2 = 0 Then
Getstr = "女" + Getstr
Else
Getstr = "男" + Getstr
End If
m = m \2
Next j
List1.AddItem Getstr
If Team(Getstr) <> "" Then List2.AddItem (Team(Getstr))
Getstr =""
End If
Next i
End Sub
Function search(i As Integer) As Boolean '判读男生是否相邻
Dim j As Integer,t As Integer
Dim a(1 To 15) As Integer
t = i
For j = 1 To n
a(j) = t Mod 2
t = t \2
Next j
flag = True
For k = 1 To n -1
If Then flag = False: Exit For
Next k
search = flag
End Function
Function Team(ch As String) As String '判读男女生比例是否合适
Dim num As Integer, st As Integer, i As Integer,c(0 To 1) As Integer
Team = “”
For i = 0 To 1
c(i) = 0
Next i
For i = 1 To n
st= Mid(ch, i, 1)
If st= "男" Then num=1: c(num)= c(num)+1 Else num =0: c(num)= c(num)+1
Next i
IfThen Team = ch
End Function