Question: Give you five numbers, and add '+', '-', '*' among them. Result 23 is displayed.
Analysis: search, a 24-point similarity.
First, obtain the full arrangement of the five numbers;
Then, enumerate the three operations in order, calculate the results, and judge.
Note: The priority is high on the left.
# Include <iostream> # include <cstdlib> # include <cstdio> using namespace STD; int data [5], save [5], used [5], Buf [5]; int P [120] [5]; int p_count; // generate void makep (INT d) {If (D = 5) {for (INT I = 0; I <5; ++ I) P [p_count] [I] = save [I]; p_count ++; return ;}for (INT I = 0; I <5; ++ I) if (! Used [I]) {used [I] = 1; save [d] = I; makep (D + 1); used [I] = 0 ;}} // calculate the 24-point similarity bool flag; void DFS (INT d) {If (FLAG) return; If (! D) {If (BUF [0] = 23) Flag = 1; return ;}// storage status int temp [5], A, B; for (INT I = 0; I <= D; ++ I) temp [I] = Buf [I]; A = Buf [0]; B = Buf [1]; for (INT I = 1; I <D; ++ I) BUF [I] = Buf [I + 1]; Buf [0] = A + B; DFS (D-1); Buf [0] = A-B; DFS (D-1); Buf [0] = a * B; DFS (D-1 ); // trace back for (INT I = 0; I <= D; ++ I) BUF [I] = temp [I];} int main () {p_count = 0; makep (0); While (~ Scanf ("") {for (INT I = 0; I <5; ++ I) scanf ("% d", & Data [I]); if (data [0] = 0) break; flag = 0; For (INT I = 0; I <120; ++ I) {for (Int J = 0; j <5; ++ J) BUF [J] = data [p [I] [J]; DFS (4); If (FLAG) break;} If (FLAG) printf ("Possible \ n"); else printf ("impossible \ n");} return 0 ;}
Test data:
Input:
42 8 2 32 37 10 43 21 46 5 44 2 27 30 29 10 20 20 2 36 28 3 34 42 222 6 6 5 3734 3 31 18 1225 46 28 13 212 4 19 2 501 12 2 1 4948 48 42 2 111 2 43 26 330 0 0 0 0
Output:
PossiblePossibleImpossibleImpossibleImpossibleImpossibleImpossibleImpossibleImpossibleImpossibleImpossibleImpossible
Ultraviolet A 10344-23 out of 5