在这个程序中为什么会下标越界
PublicSubsushu(b()AsInteger,h()AsInteger,k)DimtAsBooleanForn=1Toit=FalseForj=2Tob(n)-...
Public Sub sushu(b() As Integer, h() As Integer, k)
Dim t As Boolean
For n = 1 To i
t = False
For j = 2 To b(n) - 1
If b(n) Mod j = 0 Then t = True: Exit For
Next j
If Not t Then
k = k + 1: h(k) = b(n)
End If
Next n
End Sub 展开
Dim t As Boolean
For n = 1 To i
t = False
For j = 2 To b(n) - 1
If b(n) Mod j = 0 Then t = True: Exit For
Next j
If Not t Then
k = k + 1: h(k) = b(n)
End If
Next n
End Sub 展开
2个回答
展开全部
这里是一份色筛法求素数的代码,修改UpTo即可调整上限,速度很快
Option Explicit
Private Sub Form_Load()
Const UpTo As Long = 100
Dim i As Long, j As Long
Dim v(3 To UpTo) As Boolean
ReDim s(0 To 0) As String
s(0) = "2"
For i = 3 To UpTo Step 2
If Not v(i) Then
For j = i * 2 To UpTo Step i
v(j) = True
Next
ReDim Preserve s(0 To UBound(s) + 1)
s(UBound(s)) = CStr(i)
End If
Next
MsgBox Join(s), , "从 1 到 " & CStr(UpTo) & " 的素数列表:"
Unload Me
End Sub
你提供的那个函数我没有看懂函数意图,总之那个函数中缺少防越界的手段吧。
Option Explicit
Private Sub Form_Load()
Const UpTo As Long = 100
Dim i As Long, j As Long
Dim v(3 To UpTo) As Boolean
ReDim s(0 To 0) As String
s(0) = "2"
For i = 3 To UpTo Step 2
If Not v(i) Then
For j = i * 2 To UpTo Step i
v(j) = True
Next
ReDim Preserve s(0 To UBound(s) + 1)
s(UBound(s)) = CStr(i)
End If
Next
MsgBox Join(s), , "从 1 到 " & CStr(UpTo) & " 的素数列表:"
Unload Me
End Sub
你提供的那个函数我没有看懂函数意图,总之那个函数中缺少防越界的手段吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询