试题 试卷
题型:单选题 题类:常考题 难易度:普通
浙江省高中信息技术 算法及其程序实现单元检测
For i=1 To 3
k=i
For j=i+1 To 6
If a(j) > a(k) Then k=j
Next j
t=a(i):a(i)=a(k):a(k)=t
Next i
数组元素a(1)到a(6)的值依次为“8,2,9,3,5,1”,经过该程序段“加工”后,数组元素a(1)到a(5)的值依次为( )
For i = 1 To 7
For j = 8 To i+1 step -1
If sz(j) < sz(j-1) Then
tmp = sz(j): sz(j) = sz(j-1): sz(j-1) = tmp
End If
数组元素sz(1)到sz(8)的原始数据依次为" 5.2,4.5,4.1,3.5,8.3,7.4,3.8,7.5"。则第3遍排序"加工"后数组元素sz(1)到sz(8)的数据依次是( )
Private Sub Command1Click()
Dim num(0 To 5) As Integer
Dim i As Integer, j As Integer
Dim t As Integer
num(0) = 4: num(1) = 7: num(2) = 2: num(3) = 0: num(4) = 3: num(5) = 9
i = 0: j = 1: t = 0
For i = 1 To 5 Step 1
For j = i + 1 To 4 Step 1
If num(i) > num(j) Then
t = num(i)
num(i) = num(j)
num(j) = t
For i = 1 To 5
List1.AddItem num(i)
End Sub
小张经分析,每次n堆金币如果都是从小到大,将前两堆合并成新一堆,然后,继续进行从小到大排序,如此合并成最后一堆,此时消耗体力最少,为此他编写了一个解决上述问题,求最少体力的VB程序,功能如下:窗体加载时,自动读取n堆金币的数据,依次存储在数组元素a(1)、a(2)、a(3)…中,金币堆数存储在变量n中,n堆金币的重量数据显示在列表框List1中。单击“求解”按钮Command1,程序对n堆金币进行合并处理,消耗最少的体力显示在文本框Text1中。程序运行界面如图所示。
Dim a(1 To 1000) As Single, b(1 To 1000) As Integer Private Sub Form_Load()
Randomize '生成 1000 个随机数 For i = 1 To 1000
① '产生随机数,保留二位小数
b(i) = 1 '初始排位为 1
List1.AddItem "a(" & i & ")=" & a(i) Next i
Private Sub Command1_Click()
Dim ks As Integer, js As Integer
ks = Val(Text1.Text)
js = Val(Text2.Text)
For i = ks To js
For j = ks To js
If Then b(i) = b(i) + 1 ②
Text1 Text2
List2.AddItem "a(" & i & ")" & "在 a(" & ks & ")~a(" & js & ")中排位为" & b(i) Next i
①{#blank#}1{#/blank#} ②{#blank#}2{#/blank#}
Dim n, i, j, temp As Integer
Dim a(1 To 10) As Integer
n = Val(Text1.Text)
Randomize
a(1) = Int(Rnd * 20) + 1
For i = 2 To n
a(i) = Int(Rnd * 20) + 1
If a(i) > a(i - 1) Then
temp = a(i)
bot = 1 : Top = n
Do While bot <= Top
‘(1)
If temp < a(m) Then bot = m + 1 Else Top = m - 1
Loop
For k = n To bot + 1 Step -1
‘(2)
Next k
a(bot) = temp
For j = 1 To n
List1.AddItem Str(a(j))
以上程序段运行时,为了实现上述功能,加框处代码应改正为:
⑴{#blank#}1{#/blank#};⑵{#blank#}2{#/blank#}。
a(1)=6:a(2)=8:a(3)=7:a(4)=3:a(5)=1:a(6)=2:a(7)=5:a(8)=4
i = 1: j = 8
key = a(1)
Do While i < j
Do While i < j And a(j) >= key
j = j - 1
a(i) = a(j)
Do While i < j And a(i) <= key
i = i + 1
a(j) = a(i)
a(i) = key
For i = 1 To 8
Label1.Caption=Label1.Caption++Str(a(i))
执行该程序段,标签Label1上显示的内容是( )
试题篮