求一维数组最大值

qq41251299 2008-07-10 05:52:40
求一维数组的最大值问题:
如何求一维数组最大值,尤其是如何求连续的n个数的和的最大值啊?

Public Function Max(ByRef iA()) As Double
Dim Max As Integer, iMax As Integer
Max = iA(1): iMax = 1
For i = LBound(iA()) To UBound(iA())
If iA(i) > Max Then
Max = iA(i)
iMax = i
End If
Next i
End Function
...全文
1153 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq41251299 2008-07-13
  • 打赏
  • 举报
回复
哦,找到了。
qq41251299 2008-07-13
  • 打赏
  • 举报
回复
在哪里看得分啊,
怎么没看到?
qq41251299 2008-07-13
  • 打赏
  • 举报
回复
前几天不能上网,
今天才看到,
没有测试,
但看了下感觉应该可以的,谢谢哦
分全加给你了。
datou985 2008-07-11
  • 打赏
  • 举报
回复

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



datou985 2008-07-11
  • 打赏
  • 举报
回复
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


这次OK了,最后这个GetMax函数就是取连续N个数之和的最大值
datou985 2008-07-11
  • 打赏
  • 举报
回复
晕,发完才发现理解错了需求,不好意思,哈哈
再看一下
datou985 2008-07-11
  • 打赏
  • 举报
回复

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


上面的代码就是新建一个工程
在窗体上画个command控件
然后就能实现楼主想要的功能了
其中GetMaxFromArr就是取数组中最大值的,参数lSum就是求和
qq41251299 2008-07-10
  • 打赏
  • 举报
回复
是啊,后者怎么编呢?
熊孩子开学喽 2008-07-10
  • 打赏
  • 举报
回复
"求一维数组最大值" 和 "连续的n个数的和的最大值"
是两个不同的问题
前者只要简单循环一次就可以得到,后者需要对整个数列排序

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧