题库 高中信息

题干

n个数据的冒泡排序需要经过n-1遍加工,每一遍加工自下而上比较相邻两个数据,把较小者交换到上面。小刘发现:当某一遍加工过程中没有数据交换,说明数据已经有序,无需进一步加工。为此,小刘对算法进行优化,编写了一个VB程序,功能如下:运行程序时,在列表框List1中显示排序前数据,单击“排序”按钮Commaiid1,在列表框List2 中显示这些数据按升序排序后的结果,在标签Label3中显示排序过程的加工遍数。运行效果如第16题图所示。

实现上述功能的VB代码如下,但加框处代码有错,请改正。
Dim a(1 To 8) As Integer
Dim n As Integer
Private Sub Form_Load()
'n=8,排序前数据存储在数组a中,并在列表框Listl中显示
'代码略
End Sub
Private Sub Command1_Click()
Dim flag As Boolean 'flag值为True表示一遍加工中发生过交换
i = 1
flag = True
Do While    '(1)  
flag = False
For j =" n" To i + 1 Step -1
If a(j) < a(j - 1) Then
k =" a(j):" a(j) =" a(j" - 1): a(j - 1) = k
flag = True
End If
Next j
i =" i" + 1
Loop
Label3.Caption = "排序过程的加工遍数为" +     '(2)
For i =" 1" To n
List2.AddItem Str(a(i))
Next i
End Sub
上一题 下一题 0.99难度 填空题 更新时间:2017-02-15 04:45:03

答案(点此获取答案解析)

同类题1

小王设计制作了一个VB程序用于获取英文单词,然后背诵这些英文单词。为了使背诵英文单词更有效率,小王决定要显示的英文单词要由相同的英文字幕组成。
程序执行过程如下:程序运行后,先从数据库中获取单词,然后单击“分析单词”按钮,查找出由相回字母组成的英多单词保留下来显示,例如:获取的英文单词有:tea,pea,eta,eat,help,three,there。通过分析得知tea,eta和eat是由相同字母组成的英文单词,three和there是由相同字母组成的英文单词,则这五个英文单词保留下来,显示在label1中。如图所示:

VB部分程序如下,请将程序补充完整:
Dim wordcharsort (1 To 3000)As String'存储每个英文单词字母排序后的内容
Dim words (1 To 3000) As string'存储由相同字母组成的英文单词
Dim wordscount As Integer
Dim Getwords (1 To 3000) As String
Private Sub Form_Load()
'从数据库中获取3000个英文单词存储到数组 Getwords中,代码略
End Sub
Private Sub Command1_ Click()

Dim key As Integer

For i=1 To 3000

Wordcharsort(i)=rank(Getwords(i))

Next i

For i=1 To 3000-1

k=i
For j=i+1 To 3000
If wordcharsort(j)<wordcharsort(k) Then k=j
Next j
If i<>k Then
t1= wordcharsort(i):wordcharsort(i)= wordcharsort(k):wordcharsort(k)=tl
___________
End if
Next i
For key=1 To 3000
wordscount= search( wordcharsort(key),key, wordscount)
Next key
For i=1 To wordscount
Label1. Caption= Labell. Caption+" "+words(i)
Next i
End sub
'查找相同字母组成的英文单词,存储在 words数组中
Function search(a As String, b As Integer, c As Integer)As Integer

For i=1 To 3000

flag=False

If a=wordcharsort(i) And______ Then
For j=1 To c
If words(j)=Getwords(b) Then flag=True
Next j
If flag=False Then
Words(c)=Getwords(b)
__________
End if
End If
Next i
search=c
End function
'对英文单词中的字母进行排序
Function rank(s As String)As String

Dim c(l To 45) As String

wordscount=1

For xk=1 To 45

c(xk)=" "

Next xk

For j=1 To Len(s)

c(j)= _______

Next j

For k=1 To Len (s)-1

For kk=Len(s) To k+1 Step-1

If Asc(c(kk))<Asc(c(kk-1))Then
tmp=c(kk):c(kk)=c(kk-1):c(kk-1)=tmp
End if
Next kk
Next k
For j=1 To Len(s)
rank -rank & c(i)
Next j
End function