题型:单选题 题类:常考题 难易度:普通
浙江省高中信息技术 排序算法及程序实现同步练习
Dim a(1 To 5) As Integer
a(1)=5: a(2)=10: a(3)=9: a(4)=3: a(5)=7
n=5: p=0
swap=True
Do While swap=True
swap=False
For i=1 To n-1
If a(i) > a(i+1) Then
t=a(i): a(i)=a(i+1): a(i+1)=t
swap=True
End If
Next i
If swap=True Then p=p+1
Loop
Text1.Text=p
该程序段执行后,在文本框Text1中显示的内容为( )
排序前 |
71 |
85 |
64 |
55 |
42 |
62 |
33 |
17 |
34 |
30 |
排序后 |
71 |
85 |
17 |
55 |
33 |
30 |
62 |
42 |
34 |
64 |
实现上述功能的VB程序如下,但加框处代码有错,请改正。
Const n=10
Dim a(1 To n) As Integer
Private Sub Command1_Click()
Dim i As Integer,j As Integer
Dim temp As Integer,flag As Boolean
'读取一组正整数,存储在数组a中,代码略
i=1:j=n
temp=a(j):flag=True
Do While i<j
If flag Then
If a(i) Mod 2=0 Then
a(j)=a(i)
j=j-1
flag=Not flag
Else
'{#blank#}1{#/blank#}
End If
Else
If a(j) Mod 2=1 Then
a(i)=a(j)
flag=Not flag
Else
j=j-1
End If
End If
Loop
' {#blank#}2{#/blank#}
'依次输出排序后的数据,代码略。
End Sub
试题篮