Tutorials to .com

Tutorials to .com » Dotnet » Winform » Given natural number four 1-9, calculate the number of 4 obtained after 24 +,-,*,/ computing algorithm

Given natural number four 1-9, calculate the number of 4 obtained after 24 +,-,*,/ computing algorithm

Print View , by: iSee ,Total views: 20 ,Word Count: 1726 ,Date: Wed, 26 Aug 2009 Time: 4:08 PM

First of all, let's look at the rules of the game, gives four natural number between 1-9, for example: 1,5,5,5 (this is a very classic example of an oh ^_^)。 In the middle of 1,5,5,5 +,-,*,/ to use algorithms to get the number 24. Each number can only be used once. If there is no calculation of the basic will be difficult to live there. Haha, the answer is 5 * (5-1/5). Is it classic? And its like there are 3,3,8,8.

Let us look at the specific algorithm. In general we consider this problem, are directly select to write an oversized judged. However, repetitive work is the most boring! ! Let's analyze this simple rules of the game can find a simple way.

For example: four the number of A, B, C, D, we can use F (A, B, C, D) = 24 to represent. . We can put the function F disassembled into F1 (B, C, D) = P1 (24, A). (Meaning: B, C, D between the four operations can be A, and 24 between the four operations results). Then F1 can also continue dismantling, c and D the relationship between the four operations to get results, and B and then to a four operation result. In this way, can get a very simple array of 6 * 6 * 6 = 216 kinds of results only. Of course, this is A, B, C, D combination of a fixed sequence, then the A, B, C, D from another location, but also a combination. Therefore, all results are 6 * 6 * 6 * 12. But, we ignore a situation: A and B, and C, and D, the value of the value of a further four operations, then we also need to add a group of 6 * 6 * 6 on it.

Well, not much said, and we all look at the following bar code.

Algorithm for calculating 24 ----------''''-------------------------------- -----------------
''''Algorithm Author: CSDN (penguinMII) - Penguin
''''Development time :2005-3-23
Reference to this algorithm, if''''Please keep this information
''''---------------------------------------------- -------------------------

''''On the F1 (F2 (F3 (a1, a2), a3), a4) of the variable definition
Dim f_f (0 To 5) As Double''''2 the number of operations between the results of six kinds of post -
Dim s_s (0 To 5) As String''''2, after the number of operations between the expression
Dim f_f_f (0 To 5) As Double''''No. 3 and above the number 2 after the results of the number of operations
Dim s_s_s (0 To 5) As String''''3 the number and the number of operations after the above two expressions
Dim f_f_f_f (0 To 5) As Double''''Section 4, and above the number 3 result after a few operations
Dim s_s_s_s (0 To 5) As String''''Section 4, and above the number 3 result after a few operations
''''On the F1 (F2 (a1, a2), F3 (a3, a4)) of the variable definition
Dim f_f1 (0 To 5) As Double''''3 Number 4 Number of calculation results
Dim s_s1 (0 To 5) As String''''No. 3 No. 4, the number of operations the number of post-expression
Dim f_f2 (0 To 5) As Double''''No. 1,2 and 3,4 the number of result after the number of operations
Dim s_s2 (0 To 5) As String''''No. 1,2 and 3,4 the number of expression after the number of operations

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_f (1) = x - y
s_s_s_s (1) = "(" + CStr (x) + "-" + s + ")"
f_f_f_f (2) = y - x
s_s_s_s (2) = "(" + s + "-" + CStr (x) + ")"
f_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_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_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 Command1_Click ()
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



.Net Windows development Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.