比较三个变量的大小。该怎么处理
比较三个变量的大小。
比如a(0)=7 a(1)=6 a(2)=7
则n=7
怎么样用一个最简单效率最高的方式得出这样的结果。谢谢。
------解决方案--------------------
function max(x1 as long,x2 as long)as long
if x1>x2 then
max=x1
else
max=x2
end if
end function
调用:
dim mMax as long
mMax=max(max(x1,x2),x3)
------解决方案--------------------
很显然,你需要遍历所有的元素,且需要记录其中最大者。实际上,不需要两两比较,因为元素很多时会使逻辑很混乱。
------解决方案--------------------
实在是没分可用了
Dim arry(2)
arry(0) = 7
arry(1) = 6
arry(2) = 7
Dim Max As Integer
Max = 0
Dim l
For l = 0 To UBound(arry)
If arry(l) > Max Then
Max = arry(l)
End If
Next
MsgBox "最大数是=" & Max
比如a(0)=7 a(1)=6 a(2)=7
则n=7
怎么样用一个最简单效率最高的方式得出这样的结果。谢谢。
------解决方案--------------------
function max(x1 as long,x2 as long)as long
if x1>x2 then
max=x1
else
max=x2
end if
end function
调用:
dim mMax as long
mMax=max(max(x1,x2),x3)
------解决方案--------------------
Option Explicit
Private Function MaxItem(ByRef x() As Integer) As Integer
Dim n As Integer, i As Integer
For i = LBound(x) To UBound(x)
If x(i) > n Then n = x(i)
Next i
MaxItem = n
End Function
Private Sub Command1_Click()
Dim a(2) As Integer
a(0) = 7
a(1) = 6
a(2) = 7
MsgBox MaxItem(a)
End Sub
很显然,你需要遍历所有的元素,且需要记录其中最大者。实际上,不需要两两比较,因为元素很多时会使逻辑很混乱。
------解决方案--------------------
实在是没分可用了
Dim arry(2)
arry(0) = 7
arry(1) = 6
arry(2) = 7
Dim Max As Integer
Max = 0
Dim l
For l = 0 To UBound(arry)
If arry(l) > Max Then
Max = arry(l)
End If
Next
MsgBox "最大数是=" & Max