✔ 最佳答案
你需要用巨集(Macro)幫忙:
http://www.funp.net/249848
Download "Combination.xls" 後在黃格輸入你的數字,按"分析"數字便會出現.
注:如果只有十個數字,電腦要考慮1023個組合,如果有20個數字,則要考慮1048575個組,所以我的巨集只容許20個數字,但分析時間已經很長.
另分析前我會先把數字由小到大排列好,避免有重覆組合出現.
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 6/16/2009 by yun
'
Cells(5, 5).Value = "Analysing..."
Range("B3:B22").Select
Selection.Sort Key1:=Range("B3"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Cells(1, 1).Select
r = 6
While Cells(r, 5).Value <> Empty
Cells(r, 5).Value = ""
r = r + 1
Wend
N = Cells(4, 5).Value
RSum = Cells(2, 5).Value
r = 6
For i = 0 To 2 ^ N - 1
Sum = 0
For j = 0 To N - 1
If (i And 2 ^ j) Then Sum = Sum + Cells(j + 3, 2).Value
Next j
If Sum = RSum Then
t$ = ""
For j = 0 To N - 1
If (i And 2 ^ j) Then
If t$ <> "" Then t$ = t$ & " + "
t$ = t$ & Cells(j + 3, 2).Value
End If
Next j
exist = False
For k = 6 To r - 1
If t$ = Cells(k, 5).Value Then exist = True
Next k
If exist = False Then
Cells(r, 5).Value = t$
r = r + 1
End If
End If
Next i
Cells(5, 5).Value = "Analysis completed!"
'
End Sub
2009-06-16 15:24:20 補充:
糾正了一點錯誤:
http://www.funp.net/501831
2009-06-17 09:29:23 補充:
在Excel,True是非0,False是0.
另AND是二進制bitwise AND,例:
9 AND 4的意思是1001 AND 0100 = 0000 (False)
13 AND 4的意思是1101 AND 0100 = 0100 (True)