1. 某校内活动需要n(1≤n≤10)名学生迎宾,要求是:男女生站成一排,男生不能相邻,男女生比例大于1/2。考虑到方案只包含了男和女两种情况,可以用二进制的0来代替女生,1来代替男生。当全部是0时最小,此数为0;当全部是1时最大。枚举从0到最大数的每一种情况,逐一判断是否包含男生相邻(二进制1相连),符合的0、1转换为女、男后输出,并在此基础上输出男女生比例较合适的方案(男:女>1/2)。

按上述要求,编写VB程序,功能如下:在文本框Text1中输入n的值,点击“开始枚举”按钮Command1,在列表框List1中输出所有方案,在列表框List2中输出比例合适方案。

实现上述功能的VB程序如下,回答下列问题:

(1) 如果输入的n值等于4,那么男女比例合适的方案有种。
(2) 请在划线处填入合适的代码。

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

【考点】
枚举算法及程序实现;
【答案】

您现在未登录,无法查看试题答案与解析。 登录
综合题 困难
能力提升
真题演练
换一批