修改时间:2021-05-20 浏览次数:186 类型:月考试卷
s = Text1.Text
For i = 1 To Len(s) c = Mid(s, i, 1)
s1 = s1 + c Next i Text2.Text = s1
程序运行时,在文本框Text1中输入“ABC123xyz”,在文本框Text2中输出“bcd123yza”,则加框①处的代码为( )
A.
For i = 2 To 4
j = i: flag = False
Do While j >= 2 And Not flag If a(j) < a(j - 1) Then
t = a(j): a(j) = a(j - 1): a(j - 1) = t flag = True
End If j = j - 1
Loop Next i
数组元素a(1)到a(7)的初值依次为“8,2,7,10,6,9,5,3”,则程序运行后,元素a(1)到a(7)的值依次为( )
Key = Int(Rnd * 49) * 2 + 1
s = 0: i = 1: j = 10
Do While i <= j
m = (i + j) \ 2
If Key = a(m) Then Exit Do If Key < a(m) Then
j = m - 1: s = 2 * s
Else
i = m + 1: s = 2 * s + 1
End If Loop
数组a(1)到a(10)的值依次为“2,6,7,15,20,24,27,43,52,63”,执行该程序段后,s的值不可能为( )
图b
图a
图b
图c
图d
A .on (release) {play(1);}
B .on (press) {play();}
C .on (release) { gotoAndPlay ("main");}
D .on (release) { gotoAndPlay("main",1);})
1 |
2 |
3 |
4 |
5 |
6 |
…… |
2n-1 |
2n |
性别 |
身高 |
性别 |
身高 |
性别 |
身高 |
…… |
性别 |
身高 |
该 n位学生站成一排合影:男生全部在一侧,从低到高排序;女生全部在另一侧,从高到低排(所有人的身高h为整数,且100≤h≤200),排序前显示于List1,排序后显示于List2,如图所示。
实现上述功能的VB程序如下,请回答下列问题。
Const n = 10
Dim a(1 To 2 * n) As String Private Sub Form_Load()
'读取身高和性别数据,存储在数组a中,并显示于列表框List1中,代码略
End Sub
Private Sub Comd1_Click()
Dim count As Integer, i As Integer, j As Integer count=0
For i = 1 To n-1
‘改错①
For j = n To i+ 1 Step -1
If a(2 * j - 1) = "男" Then If
Then ‘改错②
t = a(2 * j - 2): a(2 * j - 2) = a(2 * j): a(2 * j) = t
t = a(2 * j - 3): a(2 * j - 3) = a(2 * j - 1): a(2 * j - 1) = t start = j
End If
ElseIf a(2 * j - 3) = "女" Then
If Then '填空
t = a(2 * j - 2): a(2 * j - 2) = a(2 * j): a(2 * j) = t
t = a(2 * j - 3): a(2 * j - 3) = a(2 * j - 1): a(2 * j - 1) = t start = j
End If
End If
Next j i=start-1
count=count+1 Next i
For i = 1 To n
List2.AddItem a(2 * i - 1) + a(2 * i) Next i
End Sub
① ②
具体算法描述如下:
⑴将文本框Text1中的n个数据依次读取到数组a中;
⑵构造一个数组b(j),j表示升序子序列的长度,b(j)的值表示所有j长度升序序列中最小的末尾元素值。例:序列(2,6,4,5),长度为2的子序列有(2,6)、(2,4)、(2,5)、(4,5),则b(2)=4;
⑶从第1个元素开始,依次处理到第i(1≤i≤n)个元素为止,b数组所能达到的最大下标值maxlen,处理过程分两种情况:
A.a(i)>b(maxlen),则最长升序子序列的长度增加;
B.a(i)<b(maxlen),则在b数组中逆序查找到第一个b(j)>a(i)(maxlen-1≤j≤1),更新数组b中升序子序列长度为j+1时所存储的元素值。
以图中数据为例:
⑷数组b的最大下标值即为最长升序子序列的长度。实现上述过程的VB程序如下,请回答下列问题:
Private Sub Command1_Click()
Dim a(1 To 100) As Integer '存储原序列
Dim b(1 To 100) As Integer '存储各长度序列的最小末尾元素Dim s As String
Dim n As Integer, i As Integer, j As Integer, maxlen As Integer s = Text1.Text
n = 1: j = 1
For i = 1 To Len(s) c = Mid(s, i, 1)
If c = "," Then
a(n) = Mid(s, j, i - j ) n = n + 1
j = i + 1 End If
Next i
maxlen = 1: b(1) = a(1) For i = 2 To n
If a(i) > b(maxlen) Then
maxlen = maxlen + 1 b(maxlen) = a(i)
Else
j = maxlen - 1: flag = True Do While j >= 1 And flag
If a(i) > b(j) Then
flag = False End If
j = j - 1 Loop
IfThen b(1) = a(i)
End If Next i
Label1.Caption = "最长升序子序列的长度为:" + Str(maxlen)
End Sub
试题篮