试题

试题 试卷

logo

题型:综合题 题类:常考题 难易度:普通

浙江省高中信息技术 过程与自定义函数同步练习

小明编写统计字符串中出现最多的字母和数字的程序。程序运行如下:在文本框Text1中输入一个仅包含字母和数字的字符串。单击按钮Command1后,在标签Label1上显示出现最多的数字字符,在标签Label2上显示出现最多的字母字符(字母不区分大小写,例如字母G和g统计在一起)。程序运行界面如图所示。

实现上述功能的VB程序如下,请在划线处填入合适的代码。

Private Sub Command1_Click()

Dim a(1 To 10) As Integer    ‘存储0~9每个数字出现的次数

Dim b(1 To 26) As Integer    ‘存储a~z每个字母出现的次数

Dim zf As String, x As String, p As Integer

Dim maxa As Integer, maxb As Integer

For i = 1 To Len(zf)

 x = Mid(zf, i, 1)

 If pd(x) = 1 Then

  p = Asc(x) - Asc(“0”) + 1

  a(p) = a(p) + 1

 

  p = Asc(x) - Asc(“A”) + 1

  b(p) = b(p) + 1

 Else

  p = Asc(x) - Asc(“a”) + 1

  b(p) = b(p) + 1

 End If

Next i

k = a(1):maxa = 1

For i = 2 To 10

 If a(i) > k Then k = a(i):maxa = i

Next i

k = b(1) :maxb = 1

For i = 2 To 26

 IfThen k = b(i) :maxb = i

Next i

Label1.Caption =“出现最多的数字是” + Chr(maxa+ Asc(“0”)-1)

Label2.Caption = “出现最多的字母是” + Chr(maxb + Asc(“A”)-1)

End Sub

Function pd(ch As String) As Integer ’函数用于判断每个字符类型

 If ch >= “0” And ch <= “9” Then pd = 2

 If ch >= “A” And ch <= “Z” Then pd = 2

 If ch >= “a” And ch <= “z” Then pd = 3

End Function

举一反三
现有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

返回首页

试题篮