试题

试题 试卷

logo

题型:综合题 题类:模拟题 难易度:困难

浙江省五校2021届高三下学期信息技术5月联考试卷

“一笔画游戏”是指图上的所有边有且仅经过一次刚好将整个图描完。一般图由点和边组成,一条边连接两个点,一个点可以连接多条边。若某点连接的边数是奇数,则被称为奇点,反之称为偶点。如图a图中A点连接的边有3条,A点就是奇点,B点连接的边为2条,B点就是偶点。如果图中存在0个奇点或2个奇点,则能被一笔画成,否则就不能。如图a图中奇点数为2个,可以一笔画成;如b图中奇点数为4个,不可以一笔画成。

现有VB程序用于判断某图能否一笔画成,若能,则通过穷举边的顺序来找到一种合法的路径。如c图有三条边,第1条由①②相连,第2条由④③相连,第3条由③②相连,若边的顺序231就是其中一种合法的边的顺序,代表先描第2条边,再描第3边,最后描第1条边,对应的路径④→③→②→①。在程序运行过程中,在文本框Text1中输入点的个数n,表示图中有n个点编号为1~n。在文本框Text2中输入若干点的编号,每两个点代表一条边(保证输入为偶数个点),以“,”开头,点击“一笔画”按钮Command1后,在Label3中输出结果,在若能一笔画成,则输出一条路径,否则,提示“无法一笔画!”。程序界面如c图:

c图

(1)、在Text1中输入“4”,在Text2中输入“,2,3,1,2,2,4”。则Label3输出结果是
(2)、实现上述功能的VB程序如下,请在划线处填入合适的代码。

Dim n As Integer, t As Integer, m As Integer, I As Integer, j As Integer

Dim ans As Integer, u As Integer, st As Integer, p As Long

Dim a(21) As Integer, b(11) As Integer, f(11) As Integer

Dim c(11)As Integer       '用于存储构成一笔画各边的序号

Dim s As String, ss As String

Private Sub Command1_Click()

    n= Val(Text1.Text)

    s= Text2.Text: t= 0

    For j= 1 To Len(s)

        ch = Mid(s, j, 1)

        If ch = "," Then

            b(a(t)) = b(a(t)+1

            t=t+1

            a(t)= 0

        Else

            a(t)= a(t)*10 + Val(ch)

        End If

    Next j

    : m=t\2: ans=0

    st= 1

    For i= 1 To n

        If b(i) Mod 2=1 Then ans = ans+ 1: st=i

    Next i

    If ans=0 Or ans=2 Then

        For p=1 To m^m    '穷举边的顺序

            If try(p) Then

                u= st: ss = Str(u)

                For j=1 To m

                    If (a(c(j)* 2)<>u) And (a(c(j)*2-1)<>u) Then Exit For

                    If Then u= a(c(j)*2- 1) Else u=a(c(j)*2)

                    ss=ss +"->"+ Str(u)

                Next j

                If j> m Then Exit For

            End If

        Next p

    Else

        ss="无法一笔画!"

    End If

    Label3.Caption=ss

End Sub

Function try(x As Long) As Boolean      '生成边的访问顺序,并判断有无重复的边

    Dim k As Integer, y As Long

    y=x

    For k=1 To m

        c(k)=y Mod m+1: y-y\m

        If f(c(k)) = x Then Exit For

       

    Next k

    If k> m Then try=True Else try = False

End Function

举一反三
【加试题】小明编写了一个VB应用程序,用于统计英文字母的个数,当用户输入一行英文语句,可以统计其中每个字母出现的次数。程序运行时界面如图所示。

程序运行时,在Text1中输入字符串,单击“开始统计”按钮,在List1中显示统计结果。函数IsLetter(x)功能,若x是字母,则返回值为字母x在字母序列“A”~“Z”中的序号,如字母“A”的序号为0,字母“D”的序号为3。若x不是字母,则返回值为-1。

Function IsLetter(x As String) As Integer

        If "A" <= x And x <= "Z" Then

            IsLetter = Asc(x) - Asc("A")

        ElseIf "a" <= x And x <= "z" Then

            IsLetter = Asc(x) - Asc("a")

        Else

                       ①        

        End If

    End Function

    Private Sub Command1_Click()

        Dim d(25) As Integer, m As Integer, n As Integer

        Dim s As String, c As String

        List1.Clear

        For m = 0 To 25                  '初始化数组d

            d(m) = 0

        Next m

        s = Text1.Text

        For m = 1 To Len(s)              '读取字符串中的字符进行判断

            c = Mid(s, m, 1)

            n = IsLetter(c)

            If n <> -1 Then    ②  

        Next m

        For m = 0 To 25                  '输出结果

            If    ③   Then

               List1.AddItem (Chr(m + Asc("a")) & ":" & d(m))

            End If

        Next m

   End Sub

回答下列问题:

现有n根棍子,第i根棍子的长度为a<sub>1</sub>,现设计了以下VB程序段,想要从中选出三根棍子组成周长尽可能长的三角形,输出最大的周长;若无法组成三角形,则输出0。如当n=5,a={2,3,4,5,10}时,输出12,即选择了3、4、5。当n=4,a={4,5,10,20}时,无法组成三角形,输出0。实现上述功能的VB程序代码如下,但加框处代码有误,请改正。

Dim a(1 To 1000) As Integer

Dim n As Integer

Private Sub Form_Load()

'确定n的值和数组a的各个元素值,即每根棍子的长度值,代码略

End Sub

Function max (x As Integer, y As Integer) As Integer

    If x>y Then

        max=x

    Else

        max=y

    End If

End Function

Private Sub Command1_Click()

    Dim i As Integer, J As Integer, K As Integer

    Dim ans As Integer, c As Integer, longest As Integer,

    rest As Integer

    ans=0

    '让i<j<k,保证棍子不会被重复选中

    For i=1 To n

        For j=i+1 To n

            For k=j+1 To n

                c=a(i)+a(j)+a(k)

                longest=      '①

                rest=c-longest        'rest 保存最短的两条边的和

                If Then      '②

                    ans=max(ans, c)

                End If

            Next k

        Next j

    Next i

    Print ans

End Sub

返回首页

试题篮