题型:综合题 题类:常考题 难易度:困难
浙江省嘉兴市第一中学2019届高三上学期信息技术期末考试试卷
小张编写了一个2查找孪生素数对(差值为 2)的 VB 程序,功能如下:在文本框Text1中输入一个整数n(n≤1000000),单击“求素数对”按钮Command1,在列表框List1中显示2到n范围内的素数,在标签Label2中显示素数个数,在标签Label3中显示孪生素数对个数。程序运行界面如图所示。
实现上述功能的 VB 程序如下,但加框处代码有错,请改正。
Private Sub Command1_Click()
Dim n As Long, i As Long, j As Long
Dim tAs Long '存储素数个数
Dim countAs Integer '存储素数对个数
Dim p As Long
Dim a(2 To 10000000)As Integer
n = Val(Text1.Text)
For i = 2 To n
a(i) = 1
Next i
p = 2
Do While p <= Int(Sqr(n))
If a(p) = 1 Then
j = 2
Do While j <= n \ p
'(1)
j = j + 1
Loop
End If
p = p + 1
Loop
t = 0
For i = 2 To n
If a(i) = 1 Then t = t + 1
Next i
count = 0
For i = 2 To n - 2
If Then '(2)
count = count + 1
List1.AddItem ads(i, 1) & ads(i + 2, 5)
End If
Next i
Label2.Caption = "总共有" + Str(t) + "个素数"
Label3.Caption = "其中有" + Str(count) + "个孪生素数对"
End Sub
Function ads(xAs Integer, n As Integer) As String
'整数转换成长度固定的字符串,代码略
End Function
试题篮