ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > 综合应用 > 如何用Excel计算24点?

如何用Excel计算24点?

作者:绿色风 分类: 时间:2022-08-18 浏览:338
楼主
xyh9999
Q:如何用Excel计算24点?
A:用如下自定义函数
Lqc24点.rar


  1. Function Lqc24(n1x, n2x, n3x, n4x, resultx) '计算24点
  2. On Error Resume Next
  3. LqcFH = "+-*/"
  4. LqcSS = ""
  5. LqcSS1 = "="
  6. Dim s(10) As String
  7. tempSS = Val(n1x) & "|" & Val(n2x) & "|" & Val(n3x) & "|" & Val(n4x)
  8. tempSS1 = LqcPL(tempSS)
  9. tempArrSS1 = Split(tempSS1, ";")
  10. ArrLenSS1 = UBound(tempArrSS1)
  11. For ii = 0 To ArrLenSS1 Step 1
  12. tempArrSS2 = Split(tempArrSS1(ii), "|")
  13. n1 = Val(tempArrSS2(0)): n2 = Val(tempArrSS2(1)): n3 = Val(tempArrSS2(2)): n4 = Val(tempArrSS2(3)): result = Val(resultx)
  14. For i1 = 1 To 4 Step 1
  15.   For i2 = 1 To 4 Step 1
  16.     For i3 = 1 To 4 Step 1
  17.       Zf1 = Mid(LqcFH, i1, 1): Zf2 = Mid(LqcFH, i2, 1): Zf3 = Mid(LqcFH, i3, 1)
  18.       s(1) = n1 & Zf1 & n2 & Zf2 & n3 & Zf3 & n4
  19.       s(2) = IIf((Zf1 Like "[+-]"), "(", "") & n1 & Zf1 & n2 & IIf((Zf1 Like "[+-]"), ")", "") & Zf2 & IIf((Zf3 Like "[+-]"), "(", "") & n3 & Zf3 & n4 & IIf((Zf3 Like "[+-]"), ")", "")
  20.       s(3) = IIf((Zf2 Like "[+-]"), "(", "") & IIf((Zf1 Like "[+-]"), "(", "") & n1 & Zf1 & n2 & IIf((Zf1 Like "[+-]"), ")", "") & Zf2 & n3 & IIf((Zf2 Like "[+-]"), ")", "") & Zf3 & n4
  21.       s(4) = IIf((Zf1 Like "[+-]"), "(", "") & n1 & Zf1 & IIf((Zf2 Like "[+-]"), "(", "") & n2 & Zf2 & n3 & IIf((Zf2 Like "[+-]"), ")", "") & IIf((Zf1 Like "[+-]"), ")", "") & Zf3 & n4
  22.       s(5) = n1 & Zf1 & IIf((Zf3 Like "[+-]"), "(", "") & IIf((Zf2 Like "[+-]"), "(", "") & n2 & Zf2 & n3 & IIf((Zf2 Like "[+-]"), ")", "") & Zf3 & n4 & IIf((Zf3 Like "[+-]"), ")", "")
  23.       s(6) = n1 & Zf1 & IIf((Zf2 Like "[+-]"), "(", "") & n2 & Zf2 & IIf((Zf3 Like "[+-]"), "(", "") & n3 & Zf3 & n4 & IIf((Zf3 Like "[+-]"), ")", "") & IIf((Zf2 Like "[+-]"), ")", "")
  24.       s(7) = n1 & Zf1 & IIf((Zf2 Like "[+-]"), "(", "") & n2 & Zf2 & n3 & IIf((Zf2 Like "[+-]"), ")", "") & Zf3 & n4
  25.       s(8) = IIf(((Zf1 Like "[+-]") Or (Zf2 Like "[+-]")), "(", "") & n1 & Zf1 & n2 & Zf2 & n3 & IIf(((Zf1 Like "[+-]") Or (Zf2 Like "[+-]")), ")", "") & Zf3 & n4
  26.       s(9) = n1 & Zf1 & IIf(((Zf2 Like "[+-]") Or (Zf3 Like "[+-]")), "(", "") & n2 & Zf2 & n3 & Zf3 & n4 & IIf(((Zf2 Like "[+-]") Or (Zf3 Like "[+-]")), ")", "")
  27.         For i4 = 1 To 9 Step 1
  28.           If Val(Evaluate(s(i4))) = Val(result) And (Not InStr(LqcSS1, "=" & s(i4) & "=") > 0) Then
  29.             LqcSS1 = LqcSS1 & s(i4) & "="
  30.             If Err.Number > 0 Then
  31.              Err.Clear
  32.             Else
  33.                If LqcSS = "" Then
  34.                   LqcSS = s(i4) & "=" & Val(result)
  35.                Else
  36.                   If (Not InStr(LqcSS, s(i4) & "=") > 0) Then
  37.                     LqcSS = LqcSS & "或" & s(i4) & "=" & Val(result)
  38.                   End If
  39.                End If
  40.             End If
  41.           End If
  42.         Next i4
  43.     Next i3
  44.   Next i2
  45. Next i1
  46. Next ii
  47. If LqcSS = "" Then
  48.     LqcSS = "xyh9999告诉您:" & Val(n1x) & "," & Val(n2x) & "," & Val(n3x) & "," & Val(n4x) & "不可能算出" & Val(result) & "!" & Chr(13) & Chr(10)
  49. Else
  50.     LqcSS = "xyh9999计算结果:" & Chr(13) & Chr(10) & LqcSS
  51. End If
  52. Lqc24 = LqcSS
  53. End Function
  54. Function LqcPL(cSS) '排列
  55. Dim LqcA(), LqcB()
  56. TmpPL = ""
  57. If cSS = "" Then
  58.   LqcPL = TmpPL
  59.   Exit Function
  60. End If
  61. TmpArr = Split(cSS, "|")
  62. UarrLen = UBound(TmpArr)
  63. If UarrLen = 0 Then
  64.   LqcPL = TmpArr(0)
  65.   Exit Function
  66. End If
  67. ReDim LqcA(1, UarrLen)
  68. For i = 0 To UarrLen Step 1
  69.   LqcA(0, i) = TmpArr(i) & ""
  70.   For j = 0 To UarrLen Step 1
  71.     If Not j = i Then
  72.       LqcA(1, i) = LqcA(1, i) & "|" & TmpArr(j) & ""
  73.     End If
  74.   Next j
  75.   If Left(LqcA(1, i), 1) = "|" Then LqcA(1, i) = Right(LqcA(1, i), Len(LqcA(1, i)) - 1)
  76. Next i
  77. Do While 1 = 1
  78. TmpArr = Split(LqcA(1, 0), "|")
  79. UarrLen = UBound(TmpArr)
  80. If UarrLen = 0 Then
  81.   Exit Do
  82. End If
  83. UarrAlen = UBound(LqcA, 2)
  84. UarrBlen = (UarrAlen + 1) * (UarrLen + 1) - 1
  85. ReDim LqcB(1, UarrBlen)
  86. For m = 0 To UarrAlen Step 1
  87.    Temp1 = LqcA(0, m): Temp2 = LqcA(1, m)
  88.    TmpArr = Split(Temp2, "|")
  89.    UarrLen = UBound(TmpArr)
  90.    For n = 0 To UarrLen Step 1
  91.      tt = m * (UarrLen + 1) + n
  92.      LqcB(0, tt) = Temp1 & "|" & TmpArr(n)
  93.      For k = 0 To UarrLen Step 1
  94.        If Not k = n Then
  95.          LqcB(1, tt) = LqcB(1, tt) & "|" & TmpArr(k) & ""
  96.        End If
  97.      Next k
  98.      If Left(LqcB(1, tt), 1) = "|" Then LqcB(1, tt) = Right(LqcB(1, tt), Len(LqcB(1, tt)) - 1)
  99.    Next n
  100. Next m
  101. LqcA = LqcB
  102. Loop
  103. UarrAlen = UBound(LqcA, 2)
  104. For i = 0 To UarrAlen Step 1
  105. temp = LqcA(0, i) & "|" & LqcA(1, i)
  106. If Not InStr(";" & TmpPL & ";", ";" & temp & ";") > 0 Then
  107.    TmpPL = TmpPL & ";" & temp
  108. End If
  109. Next i
  110. If Left(TmpPL, 1) = ";" Then TmpPL = Right(TmpPL, Len(TmpPL) - 1)
  111. LqcPL = TmpPL
  112. End Function
2楼
larkzh
这个有趣。

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap