试题

试题 试卷

logo

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

浙江省普通高校招生信息技术选考模拟卷(一)

【加试题】字符串排序。编写VB程序,实现如下功能:程序运行时,单击命令按钮Command1 产生100个随机字符串(每个字符串长度不超过10),并显示在列表框List1中,单击命令按钮Command2对产生的100个随机字符串按权值进行降序排序,将排序结果显示在列表框List2中。每个字符串的权值定义为该字符串中ASCII码值最大的字母的ASCII码值,例如字符串“Hello”中ASCII码值最大的字母是“o”(ASCII码值为111),则字符串“Hello”的权值为111。程序运行效果如图所示。实现上述功能的VB代码如下,但加框处代码有错,请改正。

Dim a(1 To 100) As String '依次存放100 个随机字符串

Dim b(1 To 100) As Integer '依次存放每个字符串的权值

Private Sub Command1_Click() '产生100 个随机字符串

Dim i As Integer, k As Integer, t As Integer, x As Integer

Randomize

For i = 1 To 100

k = Int(Rnd * 10 + 1) : t = 0  'k 表示第i 个字符串的长度

Do While t < k '通过循环依次产生第i 个字符串a(i)的k 个字符

If Int(Rnd * 2 + 1) = 1 Then '产生一个小写字母的ASCII 码值

x = Asc("a") + Int(Rnd * 26)

Else

x = Asc("A") + Int(Rnd * 26) '产生一个大写字母的ASCII 码值

End If

                '①

If b(i) < x Then b(i) = x

t = t + 1

Loop

List1.AddItem a(i)

Next i

End Sub

Private Sub Command2_Click() '对100 个字符串按字符串权值降序排序

Dim i As Integer, j As Integer, y As Integer

Dim s As String

For i = 1 To 99

k = i

For j = i + 1 To 100

    '②

Next j

If k <> i Then

s = a(k): a(k) = a(i): a(i) = s

y = b(k): b(k) = b(i): b(i) = y

End If

Next i

For i = 1 To 100

List2.AddItem a(i)

Next i

End Sub

 ② 

举一反三
下列VB程序实现数字字母混合序列分离后分别排序,最后又合并输出。具体算法如下:在文本框Text1输入若干组混合序列,每组序列中仅包含一组字母和一个多位数字,序列之间用逗号隔开,以逗号结束。单击“排序”按钮command1,把每组序列中的字母和数字分开,并分别排序,最后在列表框list1输出。排序规则如下:所有数字按从小到大升序排序,字母序列按长度升序排序,若长度相同,直接按字母序列大小升序排序(按字母的ASCII码排序,“A”<“Z”<“a”<“z”)。实现算法的部分程序界面如图所示,VB程序代码如下,回答下列问题:

Private Sub Command1_Click()

Dim a(1 To 6) As Integer, b(1 To 6) As String, i as integer, j as integer

Dim c As String, k As Integer, tmp1 As Integer, tmp2 As String

s=text1.text

i = 1: k = 1: tmp1 = 0: tmp2 = ""

Do While i <= Len(s)

      c = Mid(s, i, 1)

      If c = "," Then

         a(k) = tmp1: b(k) = tmp2

         tmp1 = 0: tmp2 = ""

              ①    

      Else

        If  Then 

           tmp2 = tmp2 + c

        Else

         tmp1=tmp1*10+val(c)

        End If

      End If

      i = i + 1

    Loop

    For i = 1 To 5

      For j = 1 To 6 - i

        If a(j) > a(j + 1) Then tmp1 = a(j): a(j) = a(j + 1): a(j + 1) = tmp1

    If Len(b(j)) > Len(b(j + 1)) Or      ②       Then

          tmp2 = b(j): b(j) = b(j + 1): b(j + 1) = tmp2

        End If

      Next j

    Next i

    For i = 1 To 6

      List1.AddItem Str(a(i)) + b(i)

    Next i

    End Sub

返回首页

试题篮