UVA 817 - According to Bartjens(暴力搜索)

 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:

  • 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 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.

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 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 InputOutput 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;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值