Question connection: 10400-game show math
N numeric values can be added to any number, such as +,-, *, And/. A formula must be found to make the formula value equal to the target value.
Note: 1. Arithmetic Operators have no priority. They are all combined from left to right.
2. The positions between values cannot be changed. That is to say, the order of values is determined and operators can only be modified.
3. The absolute value of an operation value must not be greater than 32000.
Solution: first, we thought of recursion. If we didn't think of good pruning, we would just recursively continue and the result would have timed out. After looking at other people's question solutions, we knew we would use a memory-based search. Is to record the sum obtained by the current first n digits through any operation. If this sum is determined to be unable to reach the goal, if another different calculation method makes the sum of the First n equal to the sum previously determined, this branch is cut off because it is no longer possible.
#include <stdio.h>#include <string.h>#include <stdlib.h>const int N = 105;const int M = 32000;const char sign[] = "+-*/";int n, num[N], order, vis[N][2 * M + 10];char str[N];int count(int sum, char c, int number) { if (c == '+')return sum + number; else if (c == '-')return sum - number; else if (c == '*')return sum * number; else if (c == '/')return sum / number; return 0;}int find(int sum, int deep) { if (deep == n) {if (sum == order) { str[deep - 1] = '='; return true;}else return false; } int cur; for (int i = 0; i < 4; i++) {if (i == 3 && (num[deep] == 0)) continue;cur = count(sum, sign[i], num[deep]);if (cur < -32000 || cur > 32000)continue;if (vis[deep][cur + M])continue;if (find(cur, deep + 1)) { str[deep - 1] = sign[i]; return true;}vis[deep][cur + M] = 1; } return false;}int main () { int cas, flag; scanf("%d", &cas); while (cas--) {memset(str, 0, sizeof(str));memset(vis, 0, sizeof(vis));scanf("%d", &n);for (int i = 0; i < n; i++) scanf("%d", &num[i]);scanf("%d", &order);if (find(num[0], 1)) { for (int i = 0; i < n; i++)printf("%d%c", num[i], str[i]); printf("%d\n", order);}else printf("NO EXPRESSION\n"); } return 0;}