|
楼主 |
发表于 2017-11-1 12:46
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
我很早就写过递归算24的代码了,而且我是计算全部组合。不是只出一组解。
- Public drr(1 To 3888, 1 To 2), m, cnt, b
- Sub test2() 'by kagawa 2012/1/27
- tms = Timer
- Erase drr
- n = [a1].End(4).Row
- ReDim arr(1 To n, 1 To 2)
- For i = 1 To n
- arr(i, 1) = Cells(i, 1)
- arr(i, 2) = Cells(i, 1)
- Next
- b = [d1]
-
- m = 0: cnt = 0
- zhjs arr, n
-
- [e1] = m
- [d3].CurrentRegion.Offset(1) = ""
- If m Then [d3].Resize(m, 2) = drr
-
- MsgBox Format(Timer - tms, "0.000s ") & m & "/" & cnt
- End Sub
- Sub zhjs(arr(), n)
- ReDim brr(1 To n - 1, 1 To 2)
- For i = 1 To n - 1
- For j = i + 1 To n
- If n > 2 Then
- l = 0
- For k = 1 To n
- If k <> i And k <> j Then
- l = l + 1
- brr(l, 1) = arr(k, 1)
- brr(l, 2) = arr(k, 2)
- End If
- Next
- End If
-
- For f = 1 To 6
-
- If f = 6 And arr(i, 1) = 0 Then
- ' [f1] = [f1] + 1
- ElseIf f = 5 And arr(j, 1) = 0 Then
- ' [f1] = [f1] + 1
- Else
- brr(n - 1, 1) = js(arr(i, 1), arr(j, 1), f)
- brr(n - 1, 2) = jg(arr(i, 2), arr(j, 2), f)
- If n = 2 Then
- cnt = cnt + 1
- If Round(brr(n - 1, 1), 12) = b Then
- m = m + 1
- drr(m, 1) = brr(n - 1, 1)
- drr(m, 2) = brr(n - 1, 2)
- End If
- Else
- zhjs brr, n - 1
- End If
- End If
- Next
- Next
- Next
- End Sub
- Function js(n1, n2, f)
- Select Case f
- Case 1
- js = n1 + n2
- Case 2
- js = n1 - n2
- Case 3
- js = n2 - n1
- Case 4
- js = n1 * n2
- Case 5
- If n2 = 0 Then js = "!0" Else js = n1 / n2
- Case 6
- If n1 = 0 Then js = "!0" Else js = n2 / n1
- End Select
- End Function
- Function jg(n1, n2, f)
- Select Case f
- Case 1
- jg = "(" & n1 & "+" & n2 & ")"
- Case 2
- jg = "(" & n1 & "-" & n2 & ")"
- Case 3
- jg = "(" & n2 & "-" & n1 & ")"
- Case 4
- jg = "(" & n1 & "*" & n2 & ")"
- Case 5
- If n2 = 0 Then jg = "/Zero" Else jg = "(" & n1 & "/" & n2 & ")"
- Case 6
- If n1 = 0 Then jg = "/Zero" Else jg = "(" & n2 & "/" & n1 & ")"
- End Select
- End Function
复制代码 |
评分
-
2
查看全部评分
-
|