|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Option Explicit
-
- Sub main()
- Dim t As Double, half As Integer, count As Long, stackSum As Integer, prevNums(), results(), num As Integer
- t = Timer
- ReDim results(1 To Rows.count, 1 To 1)
-
- Const sum = 100, startNum = 1
- half = -Int(-sum / 2)
- prevNums = Array(startNum, 0)
-
- With VBA.CreateObject("Scripting.Dictionary")
- .Add startNum, ""
- Do While True
- num = prevNums(0)
- prevNums(1) = stackSum
- stackSum = stackSum + num
- If stackSum = sum Then
- count = count + 1
- results(count, 1) = VBA.Join(.keys(), ",")
- If num = sum Then Exit Do
- .Remove num
- num = .keys()(.count - 1)
- .Remove num
- stackSum = prevNums(1) - num
- num = num + 1
- If num >= half And .count = 0 Then num = sum
- ElseIf num < sum - stackSum Then
- num = num + 1
- Else
- .Remove num
- stackSum = prevNums(1)
- num = sum - stackSum
- End If
- prevNums(0) = num
- .Add num, ""
- Loop
- End With
-
-
- Columns(1).ClearContents: Range("A1").Resize(count, 1).Value = results
- Range("B1") = "结果集:" & count & "个、耗时:" & Timer - t & "秒"
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|