Option Explicit
Private Sub Command1_Click() Dim i, j, k, s As Long For i = 1 To 5 For j = 1 To 5 For k = 1 To 5 If i + j >= k And i + k >= j And j + k >= i Then s = s + 1 AddToList Sort(i, j, k) End If Next Next Next MsgBox ("可形成的三角形共有" & List2.ListCount & "个") End Sub
'排序函数,利用了List1,设计时将List1的Sorted属性设为True Private Function Sort(x, y, z) As String List1.Clear List1.AddItem x List1.AddItem y List1.AddItem z Sort = List1.List(0) & "-" & List1.List(1) & "-" & List1.List(2) End Function
'将组合添加到列表框,若已经存在则不添加 Private Sub AddToList(str As String) Dim i As Long For i = 0 To List2.ListCount If List2.List(i) = str Then Exit Sub End If Next List2.AddItem str End Sub
[此贴子已经被作者于2005-4-1 21:51:41编辑过]
如果不想通过List1来排序,用以下函数也行。 Private Function Sort(x As Integer, y As Integer, z As Integer) As String Dim temp As Integer
If x > y Then temp = x x = y y = temp End If If y > z Then temp = y y = z z = temp End If If x > y Then temp = x x = y y = temp End If Sort = x & "-" & y & "-" & z End Function