|
我把楼主的非递归算法优化一下,速度提高7%。
注:虽然速度快了一些,但是增加一些局限性,即数字个数不能超过255个,且最大数字不能大意255。反过来再看这个局限性,这个问题基本可以忽略,因为即便100数字选10个,在普通的微机上也是实现不了的。这是我的初步判断,我没敢试 ,觉得试了也没意义。
代码如下:
Sub pengxi()
aa = Timer
Dim x%, arr() As Byte
Dim i%
Dim j%
Dim jj As Long
a = [A65536].End(xlUp).Row + 1
arrx = Range("A1:A" & a)
ReDim arr(1 To UBound(arrx))
For i = 1 To UBound(arrx)
arr(i) = arrx(i, 1)
Next
z = Cells(1, 2)
ReDim arr1(1 To z + 1) As Long '存地址
ReDim arr2(1 To z + 1) '存组合
Open "d:\peng.txt" For Output As #1
For i = z To 1 Step -1 '初始化
arr1(i) = i
arr2(i) = arr2(i + 1) & " " & arr(i)
Next i
arr1(z + 1) = 1000
Do
jj = jj + 1 '输出结果
Print #1, arr2(1)
For i = 1 To z
If arr1(i + 1) - arr1(i) > 1 Then Exit For
Next i
arr1(i) = arr1(i) + 1
arr2(i) = arr2(i + 1) & " " & arr(arr1(i))
For j = i - 1 To 1 Step -1
arr1(j) = j
arr2(j) = arr2(j + 1) & " " & arr(j)
Next j
Loop While arr1(z) < a
Close #1
MsgBox "找到 " & jj & " 个解! 花费" & Format(Timer - aa, "0.00" & "保存在D:\peng.txt") & "秒"
End Sub
[ 本帖最后由 老伙计2008 于 2009-11-15 13:37 编辑 ] |
|