24-Point Game

 

Problem

The 24-point game is a classic intellectual game using poker to carry out the calculation. The game is detailed as follows: Take out the big and little jokers from a pack of pokers, and extract any four from the remaining 52 cards. Use the addition, subtraction, multiplication, division and parentheses to manipulate the numbers on the four cards (J, Q, K, and A represent 11, 12, 13, and 1 respectively) to get the result of 24. Each card must be used once, but not repeatedly.

Write a program to manipulate any four cards for the result of 24, and print out its solution in the form of text. If no solution exists, print out “No solution”.

Tip

  1. Arrange 4 numbers in different orders.

  2. In the first layer of loop, take any three from the 4 operators and arrange them in different ways.

  3. In the second layer of loop, insert the three operators into the four numbers.

  4. Enumerate the five cases of using parentheses as follows (where A, B, C, and D represent numbers, # represents different operators):

  • ((A#B)#C)#D
  • (A#(B#C))#D
  • (A#B)#(C#D)
  • A#((B#C)#D)
  • A#(B#(C#D))

Code

A B C D
1 [3,3,8,8]
2 [1234,1243,1324,1342,1423,1432,2134,2143,2314,2341,2413,2431,3124,3142,3214,3241,3412,3421,4123,4132,4213,4231,4312,4321]
3 for A2 =A1(4.(int(A3/power(10,4-#))%10)) Arrange A1’s members according to the orders list in A2
4 for 64 Randomly select 3 operators among +,-,*,/. The selected operators are allowed to be duplicate. So each number has 4 choices, that is 4×4×4, which is a total of 64 ways
5 =["+","-","*","/"](3.(int((B4-1)/power(4,3-#))%4+1)) Convert the current loop number to a quarternary number and add 1 to each digit to get the current way of arranging the operators
6 >a=B3(1),b=B3(2),c=B3(3),d=B3(4),x=C5(1),y=C5(2),z=C5(3)
7 =func(A28,[x,a,func(A28,[y,b,func(A28,[z,c,d])])]) Surround the expression A#(B#(C#D)) with parentheses to call the subprogram for computation
8 if abs(C7-24)<0.0001
9 =string(a)+x+"("+string(b)+y+"("+string(c)+z+string(d)+"))"
10 =D10|D9 Store the result of computing the expression if it is 24
11 =func(A28,[x,a,func(A28,[z,func(A28,[y,b,c]),d])]) Surround the expression A#(B#(C#D)) with parentheses to call the subprogram for computation
12 if abs(C11-24)<0.0001
13 =string(a)+x+"(("+string(b)+y+ string(c)+")"+z+string(d)+")"
14 =D14|D13 Store the result of computing the expression if it is 24
15 =func(A28,[y,func(A28,[x,a,b]),func(A28,[z,c,d])]) Surround the expression A#(B#(C#D)) with parentheses to call the subprogram for computation
16 if abs(C15-24)<0.0001
17 ="("+string(a)+x+string(b)+")"+y+"("+string(c)+z+string(d)+")"
18 =D18|D17 Store the result of computing the expression if it is 24
19 =func(A28,[z,func(A28,[y,func(A28,[x,a,b]),c]),d]) Surround the expression A#(B#(C#D)) with parentheses to call the subprogram for computation
20 if abs(C19-24)<0.0001
21 ="(("+string(a)+x+string(b)+")"+y+string(c)+")"+z+string(d)
22 =D22|D21 Store the result of computing the expression if it is 24
23 =func(A28,[z,func(A28,[x,a,func(A28,[y,b,c])]),d]) Surround the expression A#(B#(C#D)) with parentheses to call the subprogram for computation
24 if abs(C23-24)<0.0001
25 ="("+string(a)+x+"("+string(b)+y+string(c)+"))"+z+string(d)
26 =D26|D25 Store the result of computing the expression if it is 24
27 =[D10,D14,D18,D22,D26].union().id() The final result
28 func The computation has 3 parameters: the first is operator, the second is left operand, and the third is the right operand
29 if A28(1)=="+" return A28(2)+A28(3)
30 if A28(1)=="-" return A28(2)-A28(3)
31 if A28(1)=="*" return A28(2)*A28(3)
32 if A28(1)=="/" return A28(2)/A28(3)

Result

imagepng