• UVA


    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 Input Output 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
    ", cas++);
    		solve(0, 0);
    		if (f.size() == 0) 
    			printf("  IMPOSSIBLE
    ");
    		else {
    			for (set<string>::iterator it = f.begin(); it != f.end(); it++)
    				cout << "  " << *it << endl;
    		}
    	}	
    	return 0;
    }



    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    生成15位或者4位随机数 (主要用于微信支付订单号)
    支付签名 MD5Util 排序工具类
    JVM垃圾回收(GC)
    JVM内存区域
    Java实现经典七大经典排序算法
    Java设计模式之装饰者模式
    Java设计模式之单例模式
    提前批笔试一道算法题的Java实现
    Java设计模式之工厂模式
    文件上传和下载
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4829970.html
Copyright © 2020-2023  润新知