2008年12月17日 星期三

最大值、第二大值,不Sorting

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '最大值、第二大值,不Sorting
        Dim i
        Dim a() = {2, 5, 1, 3, 4}
        Dim aM1, aM2
        If a(0) > a(1) Then
            aM1 = a(0)
            aM2 = a(1)
        Else
            aM1 = a(1)
            aM2 = a(0)
        End If
        For i = 2 To 4
            If a(i) > aM1 Then
                aM2 = aM1
                aM1 = a(i)
            End If
            If a(i) < aM1 And a(i) > aM2 Then
                aM2 = a(i)
            End If
        Next
        MsgBox("最大值:" & aM1 & "第二大值:" & aM2)
    End Sub
End Class

沒有留言: