修改时间:2024-07-31 浏览次数:256 类型:期中考试
For i = 1 To
For j = To i + 1 Step -1
If d(j) <d(j - 1) Then
t = d(j): d(j) = d(j - 1): d(j - 1) = t
End If
Next j
For j = i + 1 To
If d(j) <d(j + 1) Then
t = d(j): d(j) = d(j + 1): d(j + 1) = t
End If
Next j
Next i
方框中的代码由以下三部分组成:
①n - i + 1 ②n \ 2 ③n - i
代码顺序正确的选项是( )
i = 1: j = 6: c = 0: f = False
key = Val(Text1.Text)
Do While i<= j And Not f
c = c + 1
m = Int((i + j) / 2)
If key = a(m) Then f = True
If key < a(m) Then j = m - 1 Else i = m + 1
Loop
数组元素a(1)到a(6)依次为“8,23,25,32,46,57”,文本框Text1的内容为“32”。执行程序后,下列说法不正确的是( )
图1
请回答下列问题:
图2
图1
图2
数组元素a(1)至a(5)依次为5,22,28,42,55,在a(3)处分成两段,交换位置后为:
a(1) |
a(2) |
a(3) |
a(4) |
a(5) |
28 |
42 |
55 |
5 |
22 |
旋转数组a的最小元素是5。
数组d(1)至d(5)依次为0,1,1,1,1,在d(5)处分成两段,交换位置后为:
d(1) |
d(2) |
d(3) |
d(4) |
d(5) |
1 |
0 |
1 |
1 |
1 |
旋转数组d的最小元素是0。
小明依据查找算法思想,设计一个查找旋转数组d最小元素的程序,功能如下:运行程序时,利用“添加”按钮Command1和文本框Text1依次输入旋转数组d的值,在列表List1中显示旋转数组,单击“求解”按钮Command2,在标签Label2中显示旋转数组d最小元素值。运行效果如图1所示,实现该功能的VB程序如下:
图1
Dim n As Integer
Dim d(1 To 100) As Integer '数组 d 存储待排序数据
Private Sub Command1_Click()
'按旋转数组规则将数据存入数组d中,代码略
End Sub
Private Sub Command2_Click()
Dim mid As Integer, L As Integer, R As Integer
L = 1: R = n
mid = L
Do While d(L) >= d(R)
If Then '改错
mid = R
Exit Do
End If
mid = (L + R) \ 2
If d(L)=d(R) And d(mid)=d(R) Then
mid =______________ '填空
Exit Do
End If
If d(L) < d(mid) Then
L = mid
ElseIf d(mid) < d(R) Then
R = mid
End If
Loop
Label2.Caption = "最小值为" + Str(d(mid))
End Sub
End If
Next i
End Function
原始数据:
a(1) |
a(2) |
a(3) |
a(4) |
1 |
2 |
3 |
4 |
方法一:
①向右旋转1步:
a(1) |
a(2) |
a(3) |
a(4) |
4 |
1 |
2 |
3 |
②向右旋转2步:
a(1) |
a(2) |
a(3) |
a(4) |
3 |
4 |
1 |
2 |
③向右旋转3步:
a(1) |
a(2) |
a(3) |
a(4) |
2 |
3 |
4 |
1 |
方法二:
①将a(1)值放至旋转3个位置后位置a(4):
a(1) |
a(2) |
a(3) |
a(4) |
1 |
2 |
3 |
1 |
②将原a(4)值放至旋转3个位置后位置a(3):
a(1) |
a(2) |
a(3) |
a(4) |
1 |
2 |
4 |
1 |
③将原a(3)值放至旋转3个位置后位置a(2):
a(1) |
a(2) |
a(3) |
a(4) |
1 |
3 |
4 |
1 |
④将原a(2)值放至旋转3个位置后位置a(1):
a(1) |
a(2) |
a(3) |
a(4) |
2 |
3 |
4 |
1 |
有n个数存储在数组a中,给定一个整数p(1≤p≤n),将数组a的前p个数与后n-p个数分别向右旋转k次。例如数组a的各个数值分别是1,2,3,4,5,6,7,当p=4,k=1时,旋转后的结果为4,1,2,3,7,5,6。小明为此编写了VB程序,功能如下:在文本框Text2、Text3中依次输入p和k的值,单击“旋转数组”按钮Command1后,数组旋转后结果显示在Text4中。程序运行界面如图1所示,请回答下列问题:
图1
Dim a(20) As Integer
Const n = 20
Private Sub Form_Load()
'随机产生n个[1,100]之间的随机整数,依次存储到a(1)、a(2)、…a(n)中,并在文本框Text1显示,代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, p As Integer, k As Integer,cnt As Integer,c As Integer,t As Integer
p = Val(Text2.Text)
k = Val(Text3.Text)
i = 1:start = 1:c = a(i):cnt = 1
Do While cnt<= p '方法二
i =
t = a(i)
a(i) = c
If Then
start = start + 1
i = i + 1
c = a(i)
Else
c = t
End If
cnt = cnt + 1
Loop
For i = 1 To k '方法一
temp = a(n)
For j =
a(j) = a(j - 1)
Next j
a(p + 1) = temp
Next i
For i = 1 To n
s = s + Str(a(i))
Next i
Text4.Text = s
End Sub
试题篮