题型:综合题 题类:常考题 难易度:普通
【bj】高中信息技术综合库——枚举算法2
所谓的非常幸运数是指符合下列条件的整数:
⑴该数的偶数位数字之和等于奇数位数字之和。
⑵该数的前一半的各位数字之和正好等于后一半的各位数字之和。
实现上述功能的VB程序如下,在程序划线处填入合适的代码。
Private Sub Command1_Click()
Dim a(1 To 6) As Integer, sum(1 To 4) As Integer
Dim x As Long, i As Long, count As Long, k As Long
For i=100000 To 999999
x=i
k=1
Do While x > 0
a(k)=
x=x\10
k=k+1
Loop
sum(1)=a(1) +a(2)+a(3)
sum(2)=a(4)+a(5)+a(6)
sum(4)=a(2)+a(4)+a(6)
If sum(1)=sum(2) And sum(3)=sum(4)Then
List1.AddItem Str(i)
End If
Next i
Label2.Caption="共有个数:" + str(count)
End Sub
图1
图2
程序代码如下:
Private Sub Command1_Click()
Dim i As Integer
List1.Clear '清除列表框内容
For i = 3 To 97 ___①___ If f(i) And ② Then List1.AddItem Str(i) + " 和 " + Str(i + 2) End If Next i |
End Sub
Private Function f(x As Integer) As Boolean '自定义函数f,返回值为逻辑型
Dim j As Integer
f = True
For j = 2 To Int(Sqr(x))
If x Mod j = 0 Then
f = ③ '函数返回值
Exit For '跳出For循环
End If
Next j
End Function
k = 0 '用来计数
Max = 600 '指定范围600以内
n = 9
Do While n <= Max
Loop
Label1.Caption = Str(Max) & "范围内满足问题的数有" & Str(k) & "个"
End Sub
方框中的代码由以下五部分组成:
① n=n+7
② k = k + 1
③ List1.AddItem Str(n)
④ End If
⑤ If n Mod 3 = 2 And n Mod 5 = 3 Then
代码顺序正确的选项是( )
试题篮