我这个方法耗时20ms, seedling耗时100ms
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
Dim t1 As Double
Dim t() As String
t1 = GetTickCount
txt = "3,3,2,3,2,2,2,9,2,3,6,2,18,2,77,2,4,5,7,9,0,4,3,6,8,9,0,2,4,6,8,90,6,7,84,6546,546,56,4,6,778,4,3,3,2,3,2,2,2,9,2,3,6,2,18,2,77,2,4,5,7,9,0,4,3,6,8,9,0,2,4,6,8,90,6,7,84,6546,546,56,4,6,778,4,3,3,2,3,2,2,2,9,2,3,6,2,18,2,77,2,4,5,7,9,0,4,3,6,8,9,0,2,4,6,8,90,6,7,84,6546,546,56,4,6,778,4,3,3,2,3,2,2,2,9,2,3,6,2,18,2,77,2,4,5,7,9,0,4,3,6,8,9,0,2,4,6,8,90,6,7,84,6546,546,56,4,6,778,4,3,3,2,3,2,2,2,9,2,3,6,2,18,2,77,2,4,5,7,9,0,4,3,6,8,9,0,2,4,6,8,90,6,7,84,6546,546,56,4,6,778,4,3,3,2,3,2,2,2,9,2,3,6,2,18,2,77,2,4,5,7,9,0,4,3,6,8,9,0,2,4,6,8,90,6,7,84,6546,546,56,4,6,778,4,3,3,2,3,2,2,2,9,2,3,6,2,18,2,77,2,4,5,7,9,0,4,3,6,8,9,0,2,4,6,8,90,6,7,84,6546,546,56,4,6,778,4,3,3,2,3,2,2,2,9,2,3,6,2,18,2,77,2,4,5,7,9,0,4,3,6,8,9,0,2,4,6,8,90,6,7,84,6546,546,56,4,6,778,4,3,3,2,3,2,2,2,9,2,3,6,2,18,2,77,2,4,5,7,9,0,4,3,6,8,9,0,2,4,6,8,90,6,7,84,6546,546,56,4,6,778,4,3,3,2,3,2,2,2,9,2,3,6,2,18,2,77,2,4,5,7,9,0,4,3,6,8,9,0,2,4,6,8,90,6,7,84,6546,546,56,4,6,778,4"
t() = Split(txt, ",")
For i = LBound(t()) To UBound(t())
List1.AddItem t(i)
Next
Dim p As Integer
Dim Max As Integer
Dim C As Integer
For i = 1 To List1.ListCount
If List1.List(i) <> List1.List(i - 1) Then
If C > Max Then
p = i - 1
Max = C
End If
C = 0
Else
C = C + 1
End If
Next
Debug.Print GetTickCount - t1
Debug.Print List1.List(p)
End Sub
先谢谢大家.
Triumph(无为) 你的速度较快为2.220000000001455 S
Seedling(叶子) 你的速度为 3.199999999970894 S
我自己的为 3.916529008664656 S
我用了希尔排序,太慢了,因为数据太多了呀! 而且在判断哪个的时候很浪费时间,只有相同的数>n/2的时候才较快.
Private Const txt As String = "3,3,2,3,2,2,2,9,2,3,6,2,18,2,77"
Private Sub Command2_Click()
Dim strTmp1 As String
Dim strTmp2 As String
Dim n As Long, m As Long
Dim strCurMax As String
'BeginTimer
strTmp1 = "#" & Replace(txt, ",", "##") & "#"
Do Until strTmp1 = ""
strTmp2 = Left(strTmp1, InStr(2, strTmp1, "#", vbBinaryCompare))
n = UBound(Split(strTmp1, strTmp2))
strTmp1 = Replace(strTmp1, strTmp2, "")
If n > m Then
m = n
strCurMax = strTmp2
'Debug.Print strTmp2
End If
Loop
strCurMax = Replace(strCurMax, "#", "")
'ShowTimer
MsgBox strCurMax
End Sub