Dim i As Integer, Sum As Integer
Dim a(1 To 11) As Integer
Sum = 6
a(11) = 49
For i = 10 To 1 Step -1
a(i) = a(i + 1) - 1
If a(i) Mod 3 = 0 Then Sum = Sum + a(i)
Next i
Text1.Text = Str(Sum)
该程序段运行后,文本框text1中显示的内容是( )
i = 1: n = 5
Do While i <= n
x = Int(Rnd * 9) + 1
If x Mod 2 = 1 Then
a(i) = x
Else
a(n) = x
n = n - 1
End If
i = i + 1
Loop
数组元素的初值均为0,执行该程序段后,在下列选项中a(1)至a(5)各元素值可能的是( )
图1 |
图2 |
图3 |
图a |
图b |
图c |
图d
Const n = 20
Dim a(1 To n) As Integer
Dim lena As Integer '数组a的实际长度 Dim s As String
Private Sub Form_Load()
'生成lena个非递减序列,并输出到文本框text1中,代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer i = 1
Do While i < lena
If a(i) <> a(i + 1) Then i =
Else
For j = i + 1 To lena
a(j - 1) =
Next j
lena =
End If
Loop
s = ""
For i = 1 To lena
s = s + Str(a(i))
Next i
Text2.Text = s
End Sub
1)将文本框Text1中的文章保存到字符串变量s中。
2)从左往右扫描字符串s,用字符串word存储文章中依次出现的单词,将新出现的单词自动加入单词队列dic(i)中,单词出现的频次记录到对应的num(i)中。
如下方法扫描:
①从左往右扫描,当扫描到第i个字符时,如果是字母,继续扫描,否则,跳转到②,直至扫描结束;
②将b到i-1组成一个单词word,查询单词字典dic判断是否存在,如果不存在,将新单词插入单词字字典尾部dic(k),并且记录单词频次num(k)为1,如果dic字典中存在单词word,那么该单词对应频次增加1,再跳转到①。
Private Sub Command1_Click()
Dim dic(1 To 10000) As String '存储自建单词字典
Dim num(1 To 10000) As Integer '存储单词出现的频率
Dim word As String, c As String, s As String, slen As Integer
Dim i As Integer, j As Integer, di As Integer, k As Integer
Dim nummax As Integer '记录最高的单词频率
Dim result As String '存储出现频率最高的单词串
s = Text1.Text: slen = Len(s)
j = 0 '存储每个单词的长度
k = 1 'k-1 为当前单词字典长度
nummax = 0
For i = 1 To 10000 '初始化单词字典频次
num(i) = 0
Next i
i = 1
Do While i <= slen
If c >= "a" And c <= "z" Or c >= "A" And c <= "Z" Then j = j + 1
Else
If j <> 0 Then
word = LCase(Mid(s, i - j, j)) 'LCase()函数功能:单词统一为小写
di = 1
Do While word <> dic(di) And di < k
di = di + 1
Loop
If di = k Then '单词字典插入新单词
dic(k) = word
num(k) = 1
Else
End If
'单词在单词字典中已存在,对应频次加1
If num(di) > nummax Then nummax = num(di)
j = 0
End If
End If
i = i + 1
Loop
result = ""
For j = 1 To k - 1
List1.AddItem dic(j) + "" + Str(num(j))
If Then
If result = "" Then
result = result + dic(j) Else
result = result + "," + dic(j) '如出现频率最高的单词有多个,则用逗号分隔
End If
End If
Next j
Label2.Caption = "出现频率最高的单词是:" + result
End Sub