A 24-Point Algorithm with outputs. A 24-point algorithm is output.
We mainly use the dfs algorithm. What is troublesome is how to remove duplicates. The first step of de-duplication can be to use set to save the formula, and then use extra parentheses, and then use another set to remove duplicates. However, it is still troublesome to achieve real de-duplication. If you have good suggestions, you can send a private message. For example, 1*2*3*4 and 2*3*1*4 are actually duplicate 2*(1 + 2) * 4 and 2*4*(1 + 2) is repeated.
The Code is as follows:
# Include <iostream> # include <cstdio> # include <cstring> # include <string. h >#include <algorithm> # include <set> # include <iterator> using namespace std; int cmp (int a, int B) {return a <= B ;} void moveBrak (char * str); int m [4]; // use set mainly to deduplicate set <string> ss; void dfs (int sum, int cur, int I, string strSum, string strCur) {if (I = 3) {char strTemp [100]; if (sum + cur = 24) {sprintf (strTemp, "% s + % s = 24", strSum. c_str (), strCur. c_st R (); ss. insert (strTemp);} else if (sum-cur = 24) {sprintf (strTemp, "% s-% s = 24", strSum. c_str (), strCur. c_str (); ss. insert (strTemp);} else if (sum * cur = 24) {sprintf (strTemp, "% s * % s = 24", strSum. c_str (), strCur. c_str (); ss. insert (strTemp);} else if (cur & sum % cur = 0 & sum/cur = 24) {sprintf (strTemp, "% s/% s = 24", strSum. c_str (), strCur. c_str (); ss. insert (strTemp);} return;} char strTemp [1, 100]; sprintf (strTemp, "% d", m [I + 1]); dfs (sum, cur + m [I + 1], I + 1, strSum, "(" + strCur + "+" + strTemp + ")"); dfs (sum, cur-m [I + 1], I + 1, strSum, "(" + strCur + "-" + strTemp + ")"); dfs (sum, cur * m [I + 1], I + 1, strSum, strCur + "*" + strTemp); if (m [I + 1] & cur % m [I + 1] = 0) dfs (cur/m [I + 1], cur, I + 1, strSum, strCur + "/" + strTemp); dfs (sum + cur, m [I + 1], I + 1, "(" + strSum + "+" + strCur + ")", strTemp); dfs (sum-cur, m [I + 1], I + 1, "(" + strSum + "- "+ StrCur +") ", strTemp); dfs (sum * cur, m [I + 1], I + 1, strSum +" * "+ strCur, strTemp ); if (cur & sum % cur = 0) dfs (sum/cur, m [I + 1], I + 1, strSum + "/" + strCur, strTemp);} int main () {while (~ Scanf ("% d", & m [0], & m [1], & m [2], & m [3]) {ss. clear (); sort (m, m + 4, cmp); do {char strTemp [1, 100]; sprintf (strTemp, "% d", m [0]); string strSum = strTemp, strCur; sprintf (strTemp, "% d", m [1]); strCur = strTemp; dfs (m [0], m [1], 1, strSum, strCur);} while (next_permutation (m, m + 4); set <string >:: iterator it = ss. begin (); set <string> res; // Remove invalid parentheses for (; it! = Ss. end (); it ++) {char strTemp [100]; strcpy (strTemp, it-> c_str (); moveBrak (strTemp); res. insert (strTemp);} int cnt = 0; it = res. begin (); for (; it! = Res. end (); it ++, ++ cnt) printf ("% s \ n", it-> c_str (); if (cnt) printf ("Total % d formula \ n", cnt); else printf ("no matching formula \ n");} return 0 ;} void moveBrak (char * str) {int len = strlen (str); // dev indicates the removed parentheses, left, right indicates the reserved parentheses char dev = '#', left = '$', right = '&'; for (int I = 0; I <len; ++ I) {if (str [I] = ')') {int j = I-1; for (; j> = 0; -- j) if (str [j] = '(') break; // remove bool bDev = false; if (I <len-2 & (str [I + 2] = '+' | str [I + 2] = '-') bDev = true; if (j> = 2 & (str [J-2] = '+') bDev = true; else if (j> = 2 & (str [J-2] = '-' | str [J-2] = '*' | str [J-2] = '/ ')) bDev = false; if (bDev) str [I] = str [j] = dev; else str [I] = right, str [j] = left ;}} char strTemp [100]; int k = 0; for (int I = 0; I <len; ++ I) {if (str [I] = dev) {++ I; continue;} if (str [I] = left) strTemp [k ++] = '('; else if (str [I] = right) strTemp [k ++] = ')'; else strTemp [k ++] = str [I];} strTemp [k ++] = '\ 0 '; memcpy (str, strTemp, k );}