|
' 2.66 GHz 2GM
' 对面值 <40 进行单独处理,速度略有提高,当然也可设置常量数组
' 在原数组的基础上添加 10 ^ 9 和 30000,循环 100 次稳定在 0.43 秒,平均 0.0043秒
' 10^9:平均 0.0022 秒- Sub ChangeCoin_JsXjd()
- Dim aCoins(), aRes(), nCoin, t#
- ' 可更改段
- ' 只能是Dim
- Dim sourceDict, faceValue
- Dim coinIndex&, NofFaceValue&, divsor&, changedCoin&, matchCoin&
- ' 可更改结束
- aCoins = Array(12, 123, 1000, 50000, 600000, 30000000, 10 ^ 9, 30000)
- ' =============
- ReDim aRes(UBound(aCoins))
- t = Timer
- ' 可更改段
- coinIndex = 0
- Set sourceDict = CreateObject("Scripting.Dictionary")
- ' 可更改段
- For Each nCoin In aCoins
- '....
- sourceDict(nCoin) = 1
- While sourceDict.Count > 0
- For Each faceValue In sourceDict.keys
- NofFaceValue = sourceDict(faceValue)
- sourceDict.Remove (faceValue)
- For divsor = 2 To 4
- changedCoin = faceValue \ divsor
- If changedCoin < 40 Then
- Select Case changedCoin
- Case 24, 32, 33
- matchCoin = changedCoin + 3
- Case 25, 28, 30, 34
- matchCoin = changedCoin + 2
- Case 12, 16, 18, 20, 21, 22, 26, 27, 29, 31, 35
- matchCoin = changedCoin + 1
- Case 36 To 39
- matchCoin = 41
- Case Else
- matchCoin = changedCoin
- End Select
- aRes(coinIndex) = aRes(coinIndex) + matchCoin * NofFaceValue
- Else
- sourceDict(changedCoin) = sourceDict(changedCoin) + NofFaceValue
- End If
- Next
- Next
- Wend
- coinIndex = coinIndex + 1
- Next
- ' 可更改结束
- Set sourceDict = Nothing
- ' 可更改结束
- Debug.Print "ID: " & "<JsXjd>"
- Debug.Print "Result: " & Join(aRes, ", ")
- Debug.Print "Time: " & Format(Timer - t, "0.000s")
- End Sub
复制代码 ' 对面值未作特殊处理,代码比较简洁
' 在原数组的基础上添加 10 ^ 9 和 30000,循环 100 次稳定在 0.5 秒- Sub ChangeCoin_JsXjd2()
- Dim aCoins(), aRes(), nCoin, t#
- ' 可更改段
- ' 只能是Dim
- Dim sourceDict, faceValue
- Dim coinIndex&, NofFaceValue&, divsor&, changedCoin&, matchCoin&
- ' 可更改结束
- aCoins = Array(12, 123, 1000, 50000, 600000, 30000000, 10 ^ 9, 30000)
- ' =============
- ReDim aRes(UBound(aCoins))
- t = Timer
- ' 可更改段
- coinIndex = 0
- Set sourceDict = CreateObject("Scripting.Dictionary")
- ' 可更改段
- For Each nCoin In aCoins
- '....
- sourceDict(nCoin) = 1
- While sourceDict.Count > 0
- For Each faceValue In sourceDict.keys
- NofFaceValue = sourceDict(faceValue)
- sourceDict.Remove (faceValue)
- For divsor = 2 To 4
- changedCoin = faceValue \ divsor
- If changedCoin < 12 Then
- aRes(coinIndex) = aRes(coinIndex) + changedCoin * NofFaceValue
- Else
- sourceDict(changedCoin) = sourceDict(changedCoin) + NofFaceValue
- End If
- Next
- Next
- Wend
- coinIndex = coinIndex + 1
- Next
- ' 可更改结束
- Set sourceDict = Nothing
- ' 可更改结束
- Debug.Print "ID: " & "<JsXjd2>"
- Debug.Print "Result: " & Join(aRes, ", ")
- Debug.Print "Time: " & Format(Timer - t, "0.000s")
- End Sub
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?免费注册
x
评分
-
1
查看全部评分
-
|