7,762
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Private Sub Command1_Click()
Dim arrTest(1 To 10) As Long
Dim lIndex As Long '循环计数器
Dim lSum As Long '数组内容合计
Dim lMaxValue As Long '数组中的最大值
'初始化数组,取10个随机数
For lIndex = LBound(arrTest) To UBound(arrTest)
arrTest(lIndex) = 100 * Rnd
Print arrTest(lIndex)
Next
lMaxValue = GetMaxFromArr(arrTest(), lSum)
Print "The Total of The Array is:" & lSum
Print "The Max Value of The Array is:" & lMaxValue
Print "连续5个数的合计的最大值为:"; GetMax(arrTest(), 5)
End Sub
'取数组中的最大值,并返回数组内容之和
Private Function GetMaxFromArr(ByRef arrValue() As Long, ByRef lSum As Long) As Long
Dim lMaxValue As Long
Dim lCount As Long '循环计数器
lSum = 0
For lCount = LBound(arrValue) To UBound(arrValue)
lSum = lSum + arrValue(lCount)
If lMaxValue < arrValue(lCount) Then
lMaxValue = arrValue(lCount)
End If
Next
GetMaxFromArr = lMaxValue
End Function
'取数组中连续N个数的和的最大值
Private Function GetMax(ByRef arrValue() As Long, ByVal lCount As Long) As Long
Dim lMaxValue As Long
Dim lSum As Long
Dim lFirst As Long
Dim lSecond As Long
'检查连续个数N是否大于数组长度
If lCount > UBound(arrValue) Then
GetMax = 0
MsgBox "连续个数大于数组长度,不能取值", vbInformation, "提示"
Exit Function
End If
For lFirst = 1 To UBound(arrValue) - lCount + 1
For lSecond = 0 To lCount - 1 '取得连续N个数的合计
lSum = lSum + arrValue(lSecond + lFirst)
Next
If lMaxValue < lSum Then '判断新的合计是否比原来大,以取最大值
lMaxValue = lSum
End If
lSum = 0 '合计清0
Next
GetMax = lMaxValue
End Function
Option Explicit
Private Sub Command1_Click()
Dim arrTest(1 To 10) As Long
Dim lIndex As Long '循环计数器
Dim lSum As Long '数组内容合计
Dim lMaxValue As Long '数组中的最大值
'初始化数组,取10个随机数
For lIndex = LBound(arrTest) To UBound(arrTest)
arrTest(lIndex) = 100 * Rnd
Print arrTest(lIndex)
Next
lMaxValue = GetMaxFromArr(arrTest(), lSum)
Print "The Total of The Array is:" & lSum
Print "The Max Value of The Array is:" & lMaxValue
End Sub
'取数组中的最大值,并返回数组内容之和
Private Function GetMaxFromArr(ByRef arrValue() As Long, ByRef lSum As Long) As Long
Dim lMaxValue As Long
Dim lCount As Long '循环计数器
lSum = 0
For lCount = LBound(arrValue) To UBound(arrValue)
lSum = lSum + arrValue(lCount)
If lMaxValue < arrValue(lCount) Then
lMaxValue = arrValue(lCount)
End If
Next
GetMaxFromArr = lMaxValue
End Function