flag = True : i = 1
Do While i <= 6
x = Int(Rnd * 100) + 1
If flag Or x >50 Then
a(i)=x
flag = Not flag
i = i + 1
End if
Loop
执行该程序段后,在下列选项中,a(1)~a(6)各元素值不可能的是( )
i = 1 :j = n
Do While i + 1 < j
m = (i + j) \ 2
If a(m + 1) - a(m) > a(m) - a(m - 1) Then
Else
End if
Loop
Label1.Caption="相邻两个元素的最大差值是" + Str(a(j) - a(i))
上述程序段两个方框处的语句分别为( )
图a
图b
图a
图b
小王编写的“模拟撤销”VB程序如下,文本框 Text1中的内容是撤销前字符串,文本概Text2中的内容是历史记录,单击“撤销”按钮Command1后,在标签Label1中显示撤销过程,程序运行界面如图b所示。
Private Sub Command1_Click()
Dim s As String, t As String, ch As String
Dim c As String, num As String, ss As String
Dim n As Integer, i As Integer
s = Text1.Text: t = Text2.Text
c = "": num = "": ss = s
For i = Len(t) To 1 Step -1
①
If ch >= "a" And ch <= "z" Or ch >= "A" And ch = "Z" Then
c = ch + c
ElseIf ch >= "0" And ch <= "9" Then
num = ch + num
Else
n = Val(num)
If ch = "-" Then
s = Mid(s, 1, n - 1) + ②
Else
s = Mid(s, 1, n - 1) + Mid(s, n + Len(c), Len(s) - n - Len(c) + 1)
End If
ss = ss + "→" + s
c = "": num = ""
End If
Next i
Label1.Caption = ss
End Sub
① ②
图b
’待排序数据存储在数组a中(a(1)~ a(n)),要求升序排列
For i = 1 To (n - 1) \ 2
For j = 1 To n - i * 2
If Then ‘⑴
t = a(j): a(j) = a(j + 2): a(j + 2) = t
End If
Next j
Next i
For i = 1 To n \ 2
j = 2 * i - 1
If a(j) > a(j+1) Then t = a(j): a(j) = a(j+1): a(j+1) = t
Next i
For i = Step 2 ‘⑵
t = a(i): j = i - 1
Do While t < a(j)
a(j + 1) = a(j): j = j - 1
Loop
a(j + 1) = t
Next i
⑴ ⑵
第1段是从数组首个元素开始、元素值都属于第1区间的最长连续元素段。如果首个元素不属于第1区间,则第1段元素个数为0;
第1段分割后的剩余元素,用同样的方法来依次分割第2段、第3段、…、第m段;
第m段分割后的剩余元素分割到第m+1段(剩余段)。
若第p(1≤p≤m-1)段末尾连续元素的值也同时属于第p+1区间,则这块连续元素称为第p段“重叠块”,该段其余部分称为第p段“非重叠块”。由于不存在第m+1区间,这里特别规定:第m段的全部元素都分割到m段“非重叠块”,第m段“重叠块”的元素个数为0,一个数组分段示例如下图。图中数组第1个元素10不属于第1区间[0,5],因此第1段的元素个数为0。10和20属于第2区间,而33不属于第2区间,因此第2段只包含10和20两个元素。第3段末尾连续3个元素的值也同时属于第4区间,因此,第3段“非重叠块”包含33,58,46,第3段“重叠块”包含55,62,69。
Const n = 18, m = 6
Dim a(1 To n) As Integer
Dim b(1 To 2 * m) As Integer
'b(1)、b(2)为第1区间的下限和上限, b(3)、b(4)为第2区间的下限和上限,…
Dim c(1 To 2 * m + 1) As Integer
'数组c用于保存统计结果:
'c(1)、c(2)分别存储第1段“非重叠块”和“重叠块”的元素个数,
'c(3)、c(4)分别存储第2段“非重叠块”和“重叠块”的元素个数,…
'c(2m-1)存储第m段"非重叠块"元素个数,c(2m)存储第m段"重叠块"元素个数(值为0)
'c(2m+1)存储剩余段元素个数
Private Sub Command1_Click()
Dim i As Integer, p As Integer, L As Integer, LL As Integer
‘读取n个数据并保存在数组a中,代码略
‘读取m个区间的下限和上限并保存在数组b中,代码略
For i = 1 To 2 * m + 1
c(i) = 0
Next i
i = 1: p = 1
L = 0: LL = 0
Do While ①
If IsIn(i, p) Then
If IsIn(i, p + 1) Then
LL = LL + 1
Else
②
LL = 0
End If
i = i + 1
Else
c(2 * p - 1) = L
c(2 * p) = LL
L = 0: LL = 0
p = p + 1
End If
Loop
If i <= n Then
c(2 * p - 1) = n - i + 1
Else
c(2 * p - 1) = L
c(2 * p) = LL
End If
‘输出统计结果,代码略
End Sub
‘函数IsIn用来判断a(i)值是否属于第p区间
Function IsIn(i As Integer, p As Integer) As Boolean
If p > m Then
IsIn = False
Else
If ③ Then IsIn = True Else IsIn = False
End If
End Function
① ② ③