According to Bartjens |
The wide dissemination of calculators and computers has its disadvantages. Even students in technical disciplines tend to exhibit a surprising lack of calculating ability. Accustomed to the use of calculators and computers, many of them are unable to make calculations like 7 * 8 mentally or like 13 * 17 using pencil and paper. We all know, but who cares?
Professor Bartjens cares. Professor Bartjens is a bit old fashioned. He decided to give his students some training in calculating without electronic equipment by creating a collection of calculation problems, (like 2100 - 100 = ...). To simplify grading the problems, he constructed them so that almost all of them had 2000 as an answer. Not all of them, of course. His students would be smart enough to recognize the pattern, and fill in 2000 everywhere without further thinking.
Unfortunately Professor Bartjens’ printer driver turned out to be even more old-fashioned than the professor himself, and it could not interface with his new printer. Inspecting the printed problems, he soon recognized the pattern: none of the operations was transmitted to the printer. A problem like:
2100-100=
was printed as:
2100100=
Fortunately, all the digits and the equal sign were still printed.
To make this bad situation much worse, Professor Bartjens’ source file had disappeared. So Professor Bartjens has another problem: what were his original problems? Given the fact that the answer (most likely) should be 2000, the line 2100100= could have been any one of the lines:
2100-100= 2*100*10+0= 2*100*10-0= 2*10*0100= 2*-100*-10+0=
Professor Bartjens does remember a few things about how he wrote the problems:
The input consists of one or more test cases. Each test case is a single line containing n digits ('0'–'9'), 1 ≤n ≤9, followed by an equal sign. There will not be any blanks embedded in the input, but there may be some after the equal sign.
The last test case is followed by a line containing only the equal sign. This line should not be processed.
For each test case, print the word Problem, then the number of the case, then all possible ways of inserting operators in the row of digits such that the resulting expression has the value 2000, subject to Professor Bartjens’ memory of how he wrote the problems. Use the format shown below. If there is more than one possible problem, write the problems in lexicographic order. Each possible problem should be on a new line, indented 2 spaces. If there is no solution the answer IMPOSSIBLE should be printed, indented 2 spaces.
Sample Input | Output for the Sample Input |
---|---|
2100100= 77= = |
Problem 1 2*100*10+0= 2*100*10-0= 2100-100= Problem 2 IMPOSSIBLE |
题意:给定一个数字,可以在中间插入+ - * 使得式子等于2000.
思路:暴力枚举,最多8个位置,每个位置有4种情况,最多4^8。 可行。不过要注意题目中要按字典序输出,还有一个坑点,就是至少要插入一个运算符,所以2000=是IMPOSSIBLE,fuck。
代码:
#include <stdio.h> #include <string.h> #include <string> #include <set> #include <iostream> using namespace std; const char c[3] = {'*', '+', '-'}; const int N = 25; char str[N], re[N]; set<string> output; int n, res[N], flag; void init() { output.clear(); flag = 1; n = strlen(str) - 1; } int tra() { int num[N], num1[N], ans = 0, numn = 0, i, numn1 = 0, res1[N], resn = 0, res2[N], res2n = 0; num[0] = str[0] - '0'; for (i = 0; i < n - 1; i++) { if (res[i] == 3) { if (num [numn] == 0) return 0; num[numn] = num[numn] * 10 + str[i + 1] - '0'; } else { res1[resn++] = res[i]; num[++numn] = str[i + 1] - '0'; } } num1[0] = num[0]; for (i = 0; i < resn; i ++) { if (res1[i] == 0) { num1[numn1] *= num[i + 1]; } else { res2[res2n++] = res1[i]; num1[++numn1] = num[i + 1]; } } ans = num1[0]; for (i = 0; i < res2n; i ++) { if (res2[i] == 1) ans += num1[i + 1]; else ans -= num1[i + 1]; } return ans; } void dfs(int len) { if (len == n - 1) { int num = tra(); if (num == 2000) { flag = 0; memset(re, 0, sizeof(re)); int ren = 1; re[0] = str[0]; for (int j = 0; j < len; j ++) { if (res[j] == 3) re[ren++] = str[j + 1]; else { re[ren++] = c[res[j]]; re[ren++] = str[j + 1]; } } output.insert(re); } return; } for (int i = 0; i < 4; i ++) { res[len] = i; dfs(len + 1); } } void solve() { if (strcmp(str, "2000=") == 0) { printf(" IMPOSSIBLE\n"); return; } dfs(0); if (flag) printf(" IMPOSSIBLE\n"); else { for (set<string>::iterator it = output.begin(); it != output.end(); it++) cout <<" "<<*it<<"="<<endl; } } int main() { int cas = 0; while (~scanf("%s", str) && str[0] != '=') { init(); printf("Problem %d\n", ++cas); solve(); } return 0; }