UVA - 817 According to Bartjens

教授Bartjens因打印机故障丢失了为学生准备的数学问题集,现需编写程序通过残留的数字串重建问题集,目标是使表达式结果等于2000。该任务涉及递归算法与算术表达式的解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Download as PDF


 According to Bartjens 

The wide dissemination of calculators and computers has itsdisadvantages. Even students in technical disciplinestend to exhibit a surprising lack of calculating ability. Accustomed tothe use of calculators and computers, many ofthem are unable to make calculations like 7 * 8 mentally or like 13 *17 using pencil and paper. We all know, butwho cares?

Professor Bartjens cares. Professor Bartjens is a bit old fashioned.Hedecided to give his students some training incalculating without electronic equipment by creating a collection ofcalculation problems, (like 2100 - 100 = ...). Tosimplify grading the problems, he constructed them so that almost allof them had 2000 as an answer. Not all ofthem, of course. His students would be smart enough to recognize thepattern, and fill in 2000 everywhere withoutfurther thinking.

Unfortunately Professor Bartjens’ printer driver turned out to be evenmore old-fashioned than the professor himself,and it could not interface with his new printer. Inspecting the printedproblems, he soon recognized the pattern: noneof 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 filehad disappeared. So Professor Bartjens hasanother problem: what were his original problems? Given the fact thatthe answer (most likely) should be 2000, theline 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 theproblems:

  • He is sure that whenever he wrote down a number (other than 0),itwould not start with a zero. So2*10*0100= could not have been one of his problems.
  • He also knows he never wrote the number zero as anything but 0.So hewould not have a problem like2*1000+000=.
  • He used only binary operators, not the unary minus or plus, so2*-100*-10+0= was not an option either.
  • He used the operators +, - and * only, avoiding the operator /(afterall, they were first year students).
  • He knew all problems followed the usual precedence andassociativityrules.
You are to help Professor Bartjens recover his problem set bywriting aprogram that when given a row of digits,insert one or more of the operators +, - and * in such a way that thevalue of the resulting expression equals 2000.

Input 

The input consists of one or more test cases. Each test case is asingle line containing n digits ('0'–'9'), 1 ≤n ≤9,followed by an equal sign. There will not be any blanks embedded in theinput, but there may be some after theequal sign.

The last test case is followed by a line containing only the equalsign. This line should not be processed.

Output 

For each test case, print the word Problem, then the number of thecase, then all possible ways of insertingoperators in the row of digits such that the resulting expression hasthe value 2000, subject to Professor Bartjens’memory of how he wrote the problems. Use the format shown below. Ifthere is more than one possible problem,write the problems in lexicographic order. Each possible problemshould be on a new line, indented 2 spaces. If there is no solution theanswer 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的所有的可能

思路:枚举每个位置得到如干个数字在回溯运算符得到结果,字典序输出

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <set>
using namespace std;
const int MAXN = 200;
const int INF = 0x3f3f3f3f;
typedef long long ll;

string str;
ll res[MAXN];
int N, M, op[MAXN];
set<string> f;

ll tran(int st, int ed) {
	if (str[st] == '0' && ed-st > 0)
		return -1;
	ll x = 0;
	for (int i = st; i <= ed; i++) 
		x = x*10 + str[i]-'0';
	return x;
}

void cal() {
	int m = N-1;
	int len = str.length()+N-2;
	string p(len+1, '*');
	p[len--] = '=';
	for (int i = N-1; i >= 0; i--) {
		ll x = res[i];
		if (x == 0)
			p[len--] = '0';
		while (x) {
			p[len--] = x%10+'0';
			x /= 10;
		}
		if (i) {
			if (op[m-1] == 0) {
				p[len--] = '+';
				m--;
			}
			else if (op[m-1] == 1) {
				p[len--] = '-';
				m--;
			}
			else {
				p[len--] = '*';
				m--;
			}
		}
	}
	f.insert(p);
}

void findExpression(int cur, stack<ll> s) {
	if (cur == N-1) {
		ll ans = 0;
		while (!s.empty()) {
			ans += s.top();
			s.pop();
		}
		if (N > 1 && ans == 2000)
			cal();
		return;
	}	
	for (int i = 0; i < 3; i++) {
		op[cur] = i;
		if (i == 0) {
			s.push(res[cur+1]);
			findExpression(cur+1, s);
			s.pop();
		}
		else if (i == 1) {
			s.push(-res[cur+1]);
			findExpression(cur+1, s);
			s.pop();
		}
		else {
			ll x = res[cur+1]*s.top();
			s.pop();
			s.push(x);
			findExpression(cur+1, s);
		}
	}
}

void solve(int cur, int n) {
	if (cur == str.length()-1) {
		N = n;
		stack<ll> s;
		s.push(res[0]);
		findExpression(0, s);
		return;
	}
	for (int i = cur; i < str.length()-1; i++) {
		ll x = tran(cur, i);
		if (x != -1) {
			res[n] = x;
			solve(i+1, n+1);
		}
	}
}

int main() {
	int cas = 1;
	while (cin>>str) {
		f.clear();
		if (str.length() == 1 && str[0] == '=') 
			break;
		printf("Problem %d\n", cas++);
		solve(0, 0);
		if (f.size() == 0) 
			printf("  IMPOSSIBLE\n");
		else {
			for (set<string>::iterator it = f.begin(); it != f.end(); it++)
				cout << "  " << *it << endl;
		}
	}	
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值