楼主 xyh9999 |
Q:如何用Excel计算24点? A:用如下自定义函数 Lqc24点.rar
- Function Lqc24(n1x, n2x, n3x, n4x, resultx) '计算24点
- On Error Resume Next
- LqcFH = "+-*/"
- LqcSS = ""
- LqcSS1 = "="
- Dim s(10) As String
- tempSS = Val(n1x) & "|" & Val(n2x) & "|" & Val(n3x) & "|" & Val(n4x)
- tempSS1 = LqcPL(tempSS)
- tempArrSS1 = Split(tempSS1, ";")
- ArrLenSS1 = UBound(tempArrSS1)
- For ii = 0 To ArrLenSS1 Step 1
- tempArrSS2 = Split(tempArrSS1(ii), "|")
- n1 = Val(tempArrSS2(0)): n2 = Val(tempArrSS2(1)): n3 = Val(tempArrSS2(2)): n4 = Val(tempArrSS2(3)): result = Val(resultx)
- For i1 = 1 To 4 Step 1
- For i2 = 1 To 4 Step 1
- For i3 = 1 To 4 Step 1
- Zf1 = Mid(LqcFH, i1, 1): Zf2 = Mid(LqcFH, i2, 1): Zf3 = Mid(LqcFH, i3, 1)
- s(1) = n1 & Zf1 & n2 & Zf2 & n3 & Zf3 & n4
- s(2) = IIf((Zf1 Like "[+-]"), "(", "") & n1 & Zf1 & n2 & IIf((Zf1 Like "[+-]"), ")", "") & Zf2 & IIf((Zf3 Like "[+-]"), "(", "") & n3 & Zf3 & n4 & IIf((Zf3 Like "[+-]"), ")", "")
- s(3) = IIf((Zf2 Like "[+-]"), "(", "") & IIf((Zf1 Like "[+-]"), "(", "") & n1 & Zf1 & n2 & IIf((Zf1 Like "[+-]"), ")", "") & Zf2 & n3 & IIf((Zf2 Like "[+-]"), ")", "") & Zf3 & n4
- s(4) = IIf((Zf1 Like "[+-]"), "(", "") & n1 & Zf1 & IIf((Zf2 Like "[+-]"), "(", "") & n2 & Zf2 & n3 & IIf((Zf2 Like "[+-]"), ")", "") & IIf((Zf1 Like "[+-]"), ")", "") & Zf3 & n4
- s(5) = n1 & Zf1 & IIf((Zf3 Like "[+-]"), "(", "") & IIf((Zf2 Like "[+-]"), "(", "") & n2 & Zf2 & n3 & IIf((Zf2 Like "[+-]"), ")", "") & Zf3 & n4 & IIf((Zf3 Like "[+-]"), ")", "")
- s(6) = n1 & Zf1 & IIf((Zf2 Like "[+-]"), "(", "") & n2 & Zf2 & IIf((Zf3 Like "[+-]"), "(", "") & n3 & Zf3 & n4 & IIf((Zf3 Like "[+-]"), ")", "") & IIf((Zf2 Like "[+-]"), ")", "")
- s(7) = n1 & Zf1 & IIf((Zf2 Like "[+-]"), "(", "") & n2 & Zf2 & n3 & IIf((Zf2 Like "[+-]"), ")", "") & Zf3 & n4
- s(8) = IIf(((Zf1 Like "[+-]") Or (Zf2 Like "[+-]")), "(", "") & n1 & Zf1 & n2 & Zf2 & n3 & IIf(((Zf1 Like "[+-]") Or (Zf2 Like "[+-]")), ")", "") & Zf3 & n4
- s(9) = n1 & Zf1 & IIf(((Zf2 Like "[+-]") Or (Zf3 Like "[+-]")), "(", "") & n2 & Zf2 & n3 & Zf3 & n4 & IIf(((Zf2 Like "[+-]") Or (Zf3 Like "[+-]")), ")", "")
- For i4 = 1 To 9 Step 1
- If Val(Evaluate(s(i4))) = Val(result) And (Not InStr(LqcSS1, "=" & s(i4) & "=") > 0) Then
- LqcSS1 = LqcSS1 & s(i4) & "="
- If Err.Number > 0 Then
- Err.Clear
- Else
- If LqcSS = "" Then
- LqcSS = s(i4) & "=" & Val(result)
- Else
- If (Not InStr(LqcSS, s(i4) & "=") > 0) Then
- LqcSS = LqcSS & "或" & s(i4) & "=" & Val(result)
- End If
- End If
- End If
- End If
- Next i4
- Next i3
- Next i2
- Next i1
- Next ii
- If LqcSS = "" Then
- LqcSS = "xyh9999告诉您:" & Val(n1x) & "," & Val(n2x) & "," & Val(n3x) & "," & Val(n4x) & "不可能算出" & Val(result) & "!" & Chr(13) & Chr(10)
- Else
- LqcSS = "xyh9999计算结果:" & Chr(13) & Chr(10) & LqcSS
- End If
- Lqc24 = LqcSS
- End Function
- Function LqcPL(cSS) '排列
- Dim LqcA(), LqcB()
- TmpPL = ""
- If cSS = "" Then
- LqcPL = TmpPL
- Exit Function
- End If
- TmpArr = Split(cSS, "|")
- UarrLen = UBound(TmpArr)
- If UarrLen = 0 Then
- LqcPL = TmpArr(0)
- Exit Function
- End If
- ReDim LqcA(1, UarrLen)
- For i = 0 To UarrLen Step 1
- LqcA(0, i) = TmpArr(i) & ""
- For j = 0 To UarrLen Step 1
- If Not j = i Then
- LqcA(1, i) = LqcA(1, i) & "|" & TmpArr(j) & ""
- End If
- Next j
- If Left(LqcA(1, i), 1) = "|" Then LqcA(1, i) = Right(LqcA(1, i), Len(LqcA(1, i)) - 1)
- Next i
- Do While 1 = 1
- TmpArr = Split(LqcA(1, 0), "|")
- UarrLen = UBound(TmpArr)
- If UarrLen = 0 Then
- Exit Do
- End If
- UarrAlen = UBound(LqcA, 2)
- UarrBlen = (UarrAlen + 1) * (UarrLen + 1) - 1
- ReDim LqcB(1, UarrBlen)
- For m = 0 To UarrAlen Step 1
- Temp1 = LqcA(0, m): Temp2 = LqcA(1, m)
- TmpArr = Split(Temp2, "|")
- UarrLen = UBound(TmpArr)
- For n = 0 To UarrLen Step 1
- tt = m * (UarrLen + 1) + n
- LqcB(0, tt) = Temp1 & "|" & TmpArr(n)
- For k = 0 To UarrLen Step 1
- If Not k = n Then
- LqcB(1, tt) = LqcB(1, tt) & "|" & TmpArr(k) & ""
- End If
- Next k
- If Left(LqcB(1, tt), 1) = "|" Then LqcB(1, tt) = Right(LqcB(1, tt), Len(LqcB(1, tt)) - 1)
- Next n
- Next m
- LqcA = LqcB
- Loop
- UarrAlen = UBound(LqcA, 2)
- For i = 0 To UarrAlen Step 1
- temp = LqcA(0, i) & "|" & LqcA(1, i)
- If Not InStr(";" & TmpPL & ";", ";" & temp & ";") > 0 Then
- TmpPL = TmpPL & ";" & temp
- End If
- Next i
- If Left(TmpPL, 1) = ";" Then TmpPL = Right(TmpPL, Len(TmpPL) - 1)
- LqcPL = TmpPL
- End Function
|