题型:综合题 题类:常考题 难易度:普通
【bj】高中信息技术综合库——枚举算法及程序实现1
实现上述功能的VB代码如下,请完善代码:
Private Sub Command1_Click()
Dim n As Integer, i As Integer, t As Integer, sum As Long, s As String
n=Val(Text1.Text)
For i=1 To n^3 step 2 '变量i表示连续奇数的首个整数
sum=0:t=i:flag=False
Do While sum<n^3 And flag= False
sum=
t=t+2
If Then flag=True
Loop
If flag=True Then Exit For
Next i
s=Str(n)+ "^"+Str(3)+ "="
Do While sum>0
If sum= 0 Then
s=s+Str(i)
Else
s=s+Srt(i)+ "+"
End If
i=i+2
Loop
Text2.Text=s
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
试题篮