s = Text1.Text
s1 = ""
For i = 1 To Len(s) \ 2
m = Mid(s, i, 1)
n = Mid(s, n - i + 1, 1)
If m > n Then
s1 = s1 + m + n
Else
s1 = s1 + n + m
Next i
Text2.Text = s1
在Text1中输入“8523691”,则执行该程序后,Text2中输出的是( )
For i = 1 To 5
a(i) = Int(Rnd * 90) + 10
Next i
For j = 5 To 2 Step -1
If a(j) Mod 2 = 1 And a(j) < a(j - 1) Then
t = a(j): a(j) = a(j - 1): a(j - 1) = t
Else
a(j) = a(j) / 2
End If
Next j
执行该程序段后,a(1)到a(5)各元素不可能的是( )
Dim a(1 To 6) As String i = 6
Text1.Text = ""
Do While i > 1
k = i
For j = 1 To i - 1
If a(k) < a(j) Then k = j
Next j
tmp = a(k): a(k) = a(i): a(i) = tmp
Text1.Text = a(i)
i = i - 1
Loop
其中数据元素 a(1)到a(6)的值依次为“12”、“34”、“8”、“9”、“77”、“23”,执行该程序段后,文本框Text1中显示的内容为( )
Key = Int(Rnd * 25) * 2
n = 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: n = n + 1
Else
i = m + 1: n = n - 1
End If
Loop
Text2.Text = Str(n)
其中 a(1)到 a(10)数组的值分别“2,3,6,9,10,18,38,40,47,48”, 执行该程序段后,n 的值不可能的是( )
图 b
①按“学生总数”为“最小2项”进行筛选
②选择数据区域A2:G14,以“学生总数”为关键字进行升序排序
③对“城市”进行筛选,条件为“包含州”
④选择数据区域A2:G14,以“城市”为关键字升序排序
图 a
图 b
图 c
例如,共有N=5个景点,每个景点连接的下一个景点分别是 2,4,2,3,1。
景点号 | 1 | 2 | 3 | 4 | 5 |
下一景点号 | 2 | 4 | 2 | 3 | 1 |
则他可以从2号景点出发,最多可以游玩2号、3号、4号三个景点。程序代码如下:
Private Sub Command1_Click()
Dim a(1 To 100) As Integer, d(1 To 100) As Integer '数组 a 存放下一景点号
Dim jd As String, m As String, c As Integer, i As Integer
Dim s As Integer, p As Integer, k As Integer, ans As Integer
jd=Text1.Text+","
s=0 : c=0
For i=1 To Len(jd)
m=Mid(jd,i,1)
If m<>"," Then
①
Else
c=c+1 : a(c)=s: s=0
End If
Next i
For k=1 To 100 d(k)=0
Next k
ans=0: k=0
For i=1 To c '枚举起点
If d(i)=0 Then p=i
Do While p<=c
If d(p)=0 Then
k=k+1 :d(p)=k
Else
②
If y>ans Then ans=y k=0
Exit Do
End If
'改错
Loop
End If
Next i
Text2.Text=Str(ans)
End Sub
① ②
每个方块用一个代表其周围墙的数字之和(0≤p≤15)表示:1 表示西墙,2 表示北墙,4 表示东墙,8 表示南墙。城堡的内墙被计算两次,方块(1,1)的南墙同时也是方块(2,1)的北墙。城堡至少有两个房间。例如,图a的每个方块对应数字如图b所示。
图a
图b
程序运行界面如图c所示,现已知城堡地形对应的数字矩阵,要求出城堡一共有多少房间,最大 的房间有多大。小金利用深度优先搜索算法解决当 前问题,具体算法如下:
图c
在城堡中按行搜索,找到第一个未被搜索过方块, 以它为起点,分别按左、上、右、下的顺序向其四个方向试探,若发现一个方向上的方块是未被搜索过且可以通往的(无墙),则以这个方块为新起点,再重复上述试探。若当前方块四个方向上均无路可走,则返回上一个方块进行其他方向上的搜索,直至返回开始当前搜索的第一个方块且这个方块四个方向上也无路可走是,则本轮搜索结束。
Const m = 4 Const n = 7
Dim a(1 To m * n * 4) As Integer
Dim f(1 To m * n) As Boolean
Dim c(1 To m * n) As Integer
Private Sub Command1_Click()
Dim i As Integer, j As Integer, x As Integer, y As Integer
Dim area As Integer, max As Integer, cnt As Integer
'城堡地形对应的数字矩阵,存入 c 数组中,并显示在列表框 List1 中,f 数组初值为 false, 代码略
For i = 1 To m * n
x = c(i)
For j = 1 To 4
a((i - 1) * 4 + j) = x Mod 2
x = x \ 2
Next j
Next i
max = 0
For i = 1 To m
For j = 1 To n
IfThen
cnt = cnt + 1
area = Search(i, j)
If max < area Then
max = area
End If
Next j
Next i
Label1.Caption = "城堡一共有" + Str(cnt) + "个房间,最大的房间占" + Str(max) + "块方格."
End Sub
'从方块(x, y)开始搜索,并返回其所在房间所占方块数.
Function Search(ByVal x As Integer, ByVal y As Integer) As Integer
Dim i As Integer, j As Integer, sum As Integer, r As Integer, c As Integer
Dim pre(1 To m * n) As Integer '记录当前房间搜索的路径
Dim row(1 To 4) As Integer, col(1 To 4) As Integer
row(1) = 0: row(2) = -1: row(3) = 0: row(4) = 1
col(1) = -1: col(2) = 0: col(3) = 1: col(4) = 0
sum = 1: j = 1: pre(1) = x * 10 + y
f((x - 1) * n + y) = True
Do While True
For i = 1 To 4
r = x + row(i): c = y + col(i)
If r >= 1 And r <= m And c >= 1 And c <= n Then
If f((r - 1) * n + c) = False AndThen
sum = sum + 1
x = r: y = c
f((x - 1) * n + y) = True
j = j + 1
pre(j) = x * 10 + y
Exit For
End If
End If
Next i
If i > 4 Then
If j = 0 Then
Exit Do
x = pre(j) \ 10: y = pre(j) Mod 10
End If
Loop
Search = sum
End Function