Dim s(1 To 81) As Integer, i As Integer, j As Integer, c As Integer, temp As Integer
c=0 : s(1) = 0
For i = 1 To 9
For j = 1 To i
temp = s(j * i)
s(j * i) = j * i
If temp <> s(j * i) And s(j * i) Mod 9 = 0 Then c = c + 1
Next j
Next i
该程序段运行后,变量C的值为( )
Dim a(1 To 100) As Integer
Dim b(1 To 100) As Integer
Dim c As Integer
a(1) = 0: a(2) = 2: b(1) = 1: b(2) = 2: c = 0
For i = 2 To 6
a(i + 1) = 2 * a(i) - a(i - 1)
b(i + 1) = b(i) ^ 2 / b(i - 1)
c = c + a(i + 1) + b(i + 1)
Next i
Label1.Caption = Str(c)
该程序运行后,Label1中显示的内容是( )
key = Val(Text1.Text)
i = 1: j = n * 2 : flag = False
Do While i + 1 <= j And Not flag
m = (i + j) \ 2
If ① Then m=m-1
If a(m) = key Or a(m + 1) = key Then
flag = True
ElseIf a(m) > key Then
②
Else
③
End If
Loop
If a(i) = key Or a(j) = key Then flag = True
If flag Then Label1.Caption = "yes" Else Label1.Caption = "no"
划线处的代码正确的是( )
图b
图b
图c
例如:n=3时,第1次取1根,第2次取2根,此时正好取完,无须添加火柴,k=0;
例如:n=8时,第1次取1根,第2次取2根,第3次取3根,第4次要取4根,但剩余2根不够取。则加上1根,剩余3根,再从1根开始取,即第4次取1根,第5次取2根,剩余火柴为0,游戏结束,得k=1。
又如:n=4时,第1次取1根,第2次取2根,第3次不够取,若加上1根,剩余2根,再从1根开始取,第3次取1根,剩余1根不够第4次取,再加1根,又是剩余2根,显然加1根永远不可能取完。所以改用每次加2根,第1次取1根,第2次取2根,第3次不够取,添加2根,第3次取1根,第4次取2根,恰好能取完,得k=2。
编写一个VB程序可实现上述功能:在文本框Text1中输入火柴数量,单击“计算”按钮,在文本框Text2中显示最少添加数量,程序运行界面如下图所示:
实现上述功能的程序代码如下:
Private Sub ok_Click()
Dim a(1 To 100) As Boolean, n As Integer, k As Integer, i As Integer
n = Val(Text1.Text)
For i = 1 To 100
a(i) = False
Next i
a(n) = True
Do While n <> 0
i = 1
Do While i <= n
①
i = i + 1
Loop
If n = 0 Then
Text2.Text = Str(k)
Else
If k = 0 Then k = 1
n = n + k
If ② Then
k = k + 1
n = Val(Text1.Text)
Else
a(n) = True
End If
End If
Loop
End Sub
① ②
Const rs = 2500 '考生总人数(即记录总数)
Dim kh(1 To rs) As String, cj(0 To rs) As Integer
Dim xx(1 To 9) As String '数组xx存放学校名称,如xx(1)="柯中",xx(2)="鲁高"……数组下标代表该学校代码,即"柯中"代码是1,"鲁高"代码是2……
'从数据库中读取考号和成绩数据,存入kh和cj数组,并在list1中显示,代码略。
Private Sub Command1_Click()
Dim school As String, n As Integer, i As Integer, j As Integer
Dim t1 As Integer, t2 As String, flag As Boolean
xx(1) = "柯中": xx(2) = "鲁高": xx(3) = "越崎": xx(4) = "鉴湖"
xx(5) = "钱清": xx(6) = "豫才": xx(7) = "轻纺城": xx(8) = "鲁中"
For i = 1 To 8
If xx(i) = Text1.Text Then ①
Next i
n = Val(Text2.Text) : i = 1: flag = True
Do While flag
k = i
For j = i + 1 To rs
t = Val(Mid(kh(j), 1, 1))
If t = school Then
If ② Then k = j
End If
Next j
If i > n And cj(k) <> cj(i - 1) Then flag = False
If k <> i Then
t1 = cj(i): cj(i) = cj(k): cj(k) = t1
t2 = kh(i): kh(i) = kh(k): kh(k) = t2
End If
i = i + 1
Loop
List2.Clear
For j = 1 To ③
List2.AddItem kh(j) + " " + Str(cj(j))
Next j
End Sub
① ② ③