|
新手,不会递归,用普通算法写了一个,但是不知道具体有多少组,就随便写了个30组,大概6秒
- Sub 凑数()
- t1 = Timer
- Dim Arr, a&, b&, i%, Brr(), Crr()
- Dim Coll1 As New Collection, coll2 As New Collection
- row1 = ActiveSheet.Cells.Find("*", ActiveSheet.Cells(1, 1), xlValues, xlWhole, xlByRows, xlPrevious).Row
- Arr = Range("a1:a" & row1)
- a = 88
- For i = 1 To UBound(Arr)
- Coll1.Add Arr(i, 1), CStr(i)
- ' ReDim Preserve brr(1 To i)
- ' brr(i) = Coll.Item(i)
- Next i
- Dim dic As New Dictionary, dic2 As New Dictionary
- 'Set Coll2 = Coll1
- Do
- DoEvents
- b = Application.WorksheetFunction.RandBetween(5, 13)
- Do
- DoEvents
- bb = Application.WorksheetFunction.RandBetween(1, UBound(Arr))
- If Not dic.Exists(bb) Then
- dic.Add bb, i
- End If
- Loop Until dic.Count = b
- For Each Key In dic.Keys
- coll2.Add Key
- Next Key
- ReDim Brr(1 To b)
- For i = 1 To b
- Brr(i) = Coll1(coll2(i))
- Next i
- Str1 = Join(Brr, "+")
- sm = Evaluate(Str1)
- If sm = a Then
- If Not dic2.Exists(Str1) Then
- dic2.Add Str1, 1
- End If
- ReDim Crr(1 To dic2.Count)
- Crr = dic2.Keys
- ' Exit Do
- End If
- dic.RemoveAll
- Set coll2 = Nothing
- Erase Brr
- Loop Until dic2.Count = 30
- Columns(3).Clear
- Range("c1").Resize(UBound(Crr)) = Application.WorksheetFunction.Transpose(Crr)
- t = Timer - t1
- MsgBox t
- End Sub
复制代码
|
|