返回列表 上一主題 發帖

[發問] 數值排列組合問題

回復 1# qmi
  1. Sub Test()
  2.     Dim arInput, flagTake
  3.     Dim dResult As Object: Set dResult = CreateObject("scripting.dictionary")
  4.    
  5.     arInput = Range("A1").CurrentRegion.Value
  6.     ReDim flagTake(LBound(arInput, 2) To UBound(arInput, 2)) As Boolean 'true=取 ; false=不取
  7.    
  8.     backtrack arInput, flagTake, 1, 0, dResult
  9.    
  10.     Range("E:F").ClearContents
  11.     Range("E4").Resize(dResult.count).Value = Application.Transpose(dResult.keys)
  12.     Range("F4").Resize(dResult.count).Value = Application.Transpose(dResult.items)
  13. End Sub

  14. Sub backtrack(ByRef arInput, ByRef flagTake, n As Integer, cost As Double, ByRef dResult As Object)
  15.     Dim i As Integer, strOut As String
  16.    
  17.     If cost > 0 And cost <= 105 Then
  18.         For i = LBound(arInput, 2) To UBound(arInput, 2)
  19.             If Len(strOut) = 0 Then
  20.                 strOut = IIf(flagTake(i), arInput(1, i), "")
  21.             Else
  22.                 strOut = strOut & IIf(flagTake(i), "+" & arInput(1, i), "")
  23.             End If
  24.         Next
  25.         dResult.Add strOut, cost    'push to result
  26.     End If
  27.    
  28.     For i = n To UBound(arInput, 2)
  29.         flagTake(i) = True
  30.         backtrack arInput, flagTake, i + 1, cost + arInput(2, i), dResult
  31.         flagTake(i) = False   '回復flag
  32.     Next
  33. End Sub
複製代碼
表達不清、題意不明確、沒附檔案格式、沒有討論問題的態度~~~~~~以上愛莫能助。

TOP

        靜思自在 : 一個人的快樂.不是因為他擁有得多,而是因為他計較得少。
返回列表 上一主題