本帖最后由 vbaplus 于 2013-1-18 13:35 编辑
香川群子 发表于 2013-1-17 15:18
嗯。
我记得我一开始也是有这一段代码的,后来自己把它删掉了。
在看了你的代码后,然后我根据我对你的思路的理解,自己尝试着写出来了,最后比对细节上和你的写法有细微差别,但测试貌似没发现什么问题,关于原始数据排序上,我这个写的是针对已已经排好序的数据进行的:- Public arr1, arr2, h%, plus%, m%, n%, k%
- Sub ghqj()
- k = 0
- n = Val([c3])
- h = Application.WorksheetFunction.Min(Val([c1]), Val([c2]))
- plus = Abs(Val([c1]) - Val([c2]))
- m = [a65536].End(3).Row
- arr1 = Range("a2:a" & m).Value
- ReDim Preserve arr1(1 To m - 1, 1 To 2)
- ReDim arr2(65536, 1)
- arr1(1, 2) = arr1(1, 1)
- For i = 2 To m - 1
- arr1(i, 2) = arr1(i - 1, 2) + arr1(i, 1)
- Next i
- Call dgh(h, "", m, 1)
- arr2(0, 0) = "r": arr2(0, 1) = "s"
- Range("g1").Resize(65536, 2).ClearContents
- Range("g1").Resize(k + 1, 2) = arr2
- End Sub
- Sub dgh(r, s$, i%, t%)
- Dim x%, y%
- If n = 0 Or t = n Then
- For x = 1 To i - 1
- If arr1(x, 1) >= r And arr1(x, 1) <= r + plus Then
- k = k + 1
- arr2(k, 0) = h - r + arr1(x, 1)
- arr2(k, 1) = s & "+" & arr1(x, 1)
- ElseIf arr1(x, 1) > r + plus Then
- Exit For
- End If
- Next x
- End If
- If t = n Then Exit Sub
- For y = i - 1 To 2 Step -1
- If arr1(y, 2) < r Then Exit For
- If arr1(y, 1) < r + plus Then
- Call dgh(r - arr1(y, 1), s & "+" & arr1(y, 1), y, t + 1)
- End If
- Next y
- End Sub
复制代码 |