UVA 817 - According to Bartjens(暴力搜索)
2013-12-27 13:44
567 查看
According to Bartjens |
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:
He is sure that whenever he wrote down a number (other than 0), it would not start with a zero. So 2*10*0100= could not have been one of his problems.
He also knows he never wrote the number zero as anything but 0. So he would not have a problem like 2*1000+000=.
He used only binary operators, not the unary minus or plus, so 2*-100*-10+0= was not an option either.
He used the operators +, - and * only, avoiding the operator / (after all, they were first year students).
He knew all problems followed the usual precedence and associativity rules.
You are to help Professor Bartjens recover his problem set by writing a program that when given a row of digits, insert one or more of the operators +, - and * in such a way that the value of the resulting expression
equals 2000.
Input
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 maybe 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.
Output
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 ProfessorBartjens’ 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 |
思路:暴力枚举,最多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
, re
;
set<string> output;
int n, res
, flag;
void init() {
output.clear();
flag = 1;
n = strlen(str) - 1;
}
int tra() {
int num
, num1
, ans = 0, numn = 0, i, numn1 = 0, res1
, resn = 0, res2
, 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;
}
相关文章推荐
- UVA - 817 According to Bartjens 暴力
- uva 817 - According to Bartjens(暴力)
- UVa 817 - According to Bartjens(暴力)
- UVa 817 According to Bartjens (暴力,DFS)
- UVA - 817 According to Bartjens
- uva 817 According to Bartjens
- UVA - 817 According to Bartjens
- UVa 817:According to Bartjens(DFS)
- According to Bartjens UVA - 817
- According to Bartjens UVA - 817
- UVALive 7416 Bringing Order to Disorder(搜索)
- uva 11210 Chinese Mahjong(暴力搜索)
- UVALive 5107 dfs暴力搜索
- Eat or Not to Eat? - UVa 10273 暴力
- Uva 725 Division (暴力搜索求解,入门题)
- uva 10273 - Eat or Not to Eat?(暴力枚举)
- UVA 10273 - Eat or Not to Eat?(暴力)
- UVA - 10273 Eat or Not to Eat? 暴力
- UVA 12649 Folding Machine(暴力搜索)
- UVALive 5844 dfs暴力搜索