24-Point Algorithm

Source: Internet
Author: User

First, let's take a look at the rules of this game and give four natural numbers between 1 and 9, for example, (this is a classic example Oh ^_^ ). In the middle of, use +,-, *,/to calculate the number of 24. Each number can only be used once. If there is no calculation, it will be difficult. Haha, the answer is 5*(5-1/5 ). Is it classic? Like it, there are 3, 3, 8, and 8.

Next we will look at the specific algorithm. Generally, when we consider such a problem, we directly write an ultra-large select statement to judge it. But repetitive work is the most boring !! Let's analyze this simple game rule and find a simple method.

For example, we can use F (A, B, C, D) = 24 to represent four numbers A, B, C, and D. So. We can split function F into F1 (B, C, D) = P1 (24, ). (Meaning: The four arithmetic operations between B, C, and D can get the four arithmetic results between A and 24 ). Then F1 can continue to be split into four arithmetic relations between C and D to get the result, and then perform a four arithmetic operation with B. In this way, we can get a simple array of 6*6*6 = 216 results. Of course, this is A combination of A, B, C, and D in A fixed order, so we can change the positions of A, B, C, and D to A combination. Therefore, there are 6*6*6*12 results. However, we ignore A situation where the values of A and B and the values of C and D perform four arithmetic operations, then we need to add a group of 6*6*6.

Well, let's talk about the following code.

------------------------------ Calculate the 24 algorithm ---------------------------
Algorithm Author: CSDN (penguinMII) -- penguin
Development Time: 2005-3-23
Keep this information if this algorithm is referenced.
-----------------------------------------------------------------------

Variable definitions for F1 (F2 (F3 (a1, a2), a3), a4)
Six results after calculation between Dim f_f (0 To 5) As Double 2 count
Expression after calculation between Dim s_s (0 To 5) As String 2
Dim f_f_f (0 To 5) As Double 3rd number and result after the above 2 number operation
Dim s_s_s (0 To 5) As String 3rd number and expression after the above 2 number operation
Dim f_f_f (0 To 5) As Double 4th count and the result after the above 3 count Calculation
Dim s_s_s_s (0 To 5) As String 4th count and the result after the above 3 count Calculation
Variable definitions for F1 (F2 (a1, a2), F3 (a3, a4)
Dim f_f1 (0 To 5) As Double 3rd count 4th count Calculation Result
Dim s_s1 (0 To 5) As String 3rd number 4th number expression after calculation
Dim f_f2 (0 To 5) As Double 1st, number 2, number 3rd, and number 4
Dim s_s2 (0 To 5) As String 1st, 2, 3rd, and 4

Sub ff2 (x As Double, y As Double, sx As String, sy As String)
On Error Resume Next
F_f2 (0) = x + y
S_s2 (0) = "(" + sx + "+" + sy + ")"
F_f2 (1) = x-y
S_s2 (1) = "(" + sx + "-" + sy + ")"
F_f2 (2) = y-x
S_s2 (2) = "(" + sy + "-" + sx + ")"
F_f2 (3) = x * y
S_s2 (3) = "(" + sx + "*" + sy + ")"
F_f2 (4) = x/y
S_s2 (4) = "(" + sx + "/" + sy + ")"
F_f2 (5) = y/x
S_s2 (5) = "(" + sy + "/" + sx + ")"

End Sub

Sub ff1 (x As Integer, y As Integer)
On Error Resume Next
F_f1 (0) = x + y
S_s1 (0) = "(" + CStr (x) + "+" + CStr (y) + ")"
F_f1 (1) = x-y
S_s1 (1) = "(" + CStr (x) + "-" + CStr (y) + ")"
F_f1 (2) = y-x
S_s1 (2) = "(" + CStr (y) + "-" + CStr (x) + ")"
F_f1 (3) = x * y
S_s1 (3) = "(" + CStr (x) + "*" + CStr (y) + ")"
F_f1 (4) = x/y
S_s1 (4) = "(" + CStr (x) + "/" + CStr (y) + ")"
F_f1 (5) = y/x
S_s1 (5) = "(" + CStr (y) + "/" + CStr (x) + ")"

End Sub

Sub ff (x As Integer, y As Integer)
On Error Resume Next
F_f (0) = x + y
S_s (0) = "(" + CStr (x) + "+" + CStr (y) + ")"
F_f (1) = x-y
S_s (1) = "(" + CStr (x) + "-" + CStr (y) + ")"
F_f (2) = y-x
S_s (2) = "(" + CStr (y) + "-" + CStr (x) + ")"
F_f (3) = x * y
S_s (3) = "(" + CStr (x) + "*" + CStr (y) + ")"
F_f (4) = x/y
S_s (4) = "(" + CStr (x) + "/" + CStr (y) + ")"
F_f (5) = y/x
S_s (5) = "(" + CStr (y) + "/" + CStr (x) + ")"

End Sub

Sub fff (x As Integer, y As Double, s As String)
On Error Resume Next
F_f_f (0) = x + y
S_s_s (0) = "(" + CStr (x) + "+" + s + ")"
F_f_f (1) = x-y
S_s_s (1) = "(" + CStr (x) + "-" + s + ")"
F_f_f (2) = y-x
S_s_s (2) = "(" + s + "-" + CStr (x) + ")"
F_f_f (3) = x * y
S_s_s (3) = "(" + CStr (x) + "*" + s + ")"
F_f_f (4) = x/y
S_s_s (4) = "(" + CStr (x) + "/" + s + ")"
F_f_f (5) = y/x
S_s_s (5) = "(" + s + "/" + CStr (x) + ")"

End Sub


Sub ffff (x As Integer, y As Double, s As String)
On Error Resume Next
F_f_f_f (0) = x + y
S_s_s_s (0) = "(" + CStr (x) + "+" + s + ")"
F_f_f (1) = x-y
S_s_s_s (1) = "(" + CStr (x) + "-" + s + ")"
F_f_f (2) = y-x
S_s_s_s (2) = "(" + s + "-" + CStr (x) + ")"
F_f_f (3) = x * y
S_s_s_s (3) = "(" + CStr (x) + "*" + s + ")"
F_f_f_f (4) = x/y
S_s_s_s (4) = "(" + CStr (x) + "/" + s + ")"
F_f_f_f (5) = y/x
S_s_s_s (5) = "(" + s + "/" + CStr (x) + ")"

End Sub

Sub ppp (a1 As Integer, a2 As Integer, a3 As Integer, a4 As Integer)
Dim tempp As Integer

Tempp = 0

Call ff (a1, a2)
For I = 0 To 5
Call fff (a3, f_f (I), s_s (I ))
For j = 0 To 5
Call ffff (a4, f_f_f (j), s_s_s (j ))
For k = 0 To 5
If f_f_f (k)> 23.99999 And f_f_f_f (k) <24.00001 Then


Tempp = 0
For xyz = 0 To Me. List1.ListCount-1
If Me. List1.List (xyz) = s_s_s_s (k) Then
Tempp = tempp + 1
End If
Next xyz

If tempp = 0 Then
Me. List1.AddItem s_s_s (k)
End If

End If
Next k
Next j
Next I

End Sub

Sub qqq (a1 As Integer, a2 As Integer, a3 As Integer, a4 As Integer)
Dim tempp As Integer
Tempp = 0

Call ff (a1, a2)
Call ff1 (a3, a4)
For I = 0 To 5
For j = 0 To 5
Call ff2 (f_f (I), f_f1 (j), s_s (I), s_s1 (j ))
For k = 0 To 5
If f_f2 (k)> 23.9999 And f_f2 (k) <24.00001 Then

Tempp = 0
For xyz = 0 To Me. List1.ListCount-1
If Me. List1.List (xyz) = s_s2 (k) Then
Tempp = tempp + 1
End If
Next xyz

If tempp = 0 Then
Me. List1.AddItem s_s2 (k)
End If

End If
Next k
Next j
Next I
End Sub

Private Sub commandementclick ()
Me. List1.Clear


Call ppp (Me. Text1 (0). Text, Me. Text1 (1). Text, Me. Text1 (2). Text, Me. Text1 (3). Text)
Call ppp (Me. Text1 (0). Text, Me. Text1 (1). Text, Me. Text1 (3). Text, Me. Text1 (2). Text)
Call ppp (Me. Text1 (0). Text, Me. Text1 (2). Text, Me. Text1 (1). Text, Me. Text1 (3). Text)
Call ppp (Me. Text1 (0). Text, Me. Text1 (2). Text, Me. Text1 (3). Text, Me. Text1 (1). Text)
Call ppp (Me. Text1 (0). Text, Me. Text1 (3). Text, Me. Text1 (1). Text, Me. Text1 (2). Text)
Call ppp (Me. Text1 (0). Text, Me. Text1 (3). Text, Me. Text1 (2). Text, Me. Text1 (1). Text)
Call ppp (Me. Text1 (1). Text, Me. Text1 (2). Text, Me. Text1 (3). Text, Me. Text1 (0). Text)
Call ppp (Me. Text1 (1). Text, Me. Text1 (2). Text, Me. Text1 (0). Text, Me. Text1 (3). Text)
Call ppp (Me. Text1 (1). Text, Me. Text1 (3). Text, Me. Text1 (0). Text, Me. Text1 (2). Text)
Call ppp (Me. Text1 (1). Text, Me. Text1 (3). Text, Me. Text1 (2). Text, Me. Text1 (0). Text)
Call ppp (Me. Text1 (2). Text, Me. Text1 (3). Text, Me. Text1 (1). Text, Me. Text1 (0). Text)
Call ppp (Me. Text1 (2). Text, Me. Text1 (3). Text, Me. Text1 (0). Text, Me. Text1 (1). Text)

Call qqq (Me. Text1 (0). Text, Me. Text1 (1). Text, Me. Text1 (2). Text, Me. Text1 (3). Text)

End Sub

 

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.