|
楼主 |
发表于 2012-10-8 17:56
|
显示全部楼层
本帖最后由 彭希仁 于 2012-10-8 17:57 编辑
Dim arr, z As Long, jj As Long, d, j%, xx
Sub peng()
Set d = CreateObject("Scripting.Dictionary")
jj = 0
Open "d:\peng.txt" For Output As #1
Columns("A:A").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo
j = [A65536].End(xlUp).Row
arr = Range("A1:B" & j)
z = Cells(1, 2)
For i = 1 To UBound(arr) - 1 '定位
d(arr(i, 1)) = i
arr(i, 2) = arr(i, 1) + arr(i + 1, 1)
Next i
d(arr(j, 2)) = arr(i, 1)
d(arr(j, 1)) = i
j = j - 1
aa = Timer
Call xi("", 1, 0)
MsgBox "找到 " & jj & " 个解! 花费" & Format(Timer - aa, "0.00" & "保存在D:\peng.txt") & "秒"
Close #1
End Sub
Sub xi(a, X As Long, Y As Long)
If Y + arr(X, 2) >= z Then '最后一个数直接定位
If d.Exists(z - Y) Then
jj = jj + 1
' Print #1, z - Y & a
End If
If Y + arr(X, 2) = z Then
jj = jj + 1
' Print #1, arr(X + 1, 1) & "+" & arr(X, 1) & a
End If
Exit Sub
End If
If X > j Then Exit Sub '递归层数
Call xi("+" & arr(X, 1) & a, X + 1, Y + arr(X, 1)) '
Call xi(a, X + 1, Y)
End Sub
再优化了一下,1-100=100 只需1.27秒 |
|