Description

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

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

UVA - 817 According to Bartjens的更多相关文章

  1. UVa 817 According to Bartjens (暴力,DFS)

    题意:给出一个数字组成的字符串,然后在字符串内添加三种运算符号 * + - ,要求输出所有添加运算符并运算后结果等于2000的式子. 所有数字不能有前导0, 且式子必须是合法的. 析:这个题很明显的暴 ...

  2. 紫书 习题7-13 UVa 817(dfs+栈求表达式的值)

    题目链接  点击打开链接 这道题分为两个部分, 一用搜索枚举每种可能, 二计算表达式的值, 有挺多细节需要注意 特别注意我的代码中在计算表达式的值中用到了一个!(代码枚举中的!表示不加符号, 我现在说 ...

  3. 紫书 习题 8-17 UVa 11536 (滑动窗口)

    这道题说连续子序列, 马上就想到滑动窗口. 注意窗口里面的元素中小于等于k的才是有效元素.记录窗口里面有效元素的个数, 满足了之后开始 缩短窗口, 如果左端点不是有效元素或者即使窗口中存在这个元素的个 ...

  4. 紫书 例题8-17 UVa 1609 (构造法)(详细注释)

    这道题用构造法, 就是自己依据题目想出一种可以得到解的方法, 没有什么规律可言, 只能根据题目本身来思考. 这道题的构造法比较复杂, 不知道刘汝佳是怎么想出来的, 我想的话肯定想不到. 具体思路紫书上 ...

  5. 【习题 8-17 UVA - 11536】Smallest Sub-Array

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 尺取法. 考虑一个1..i的窗口. 里面在到达了i位置的时候恰好有1..k这些数字了. 为了更接近答案. 显然可以试着让左端点变成2 ...

  6. UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据

    题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...

  7. UVA 1412 Fund Management (预处理+状压dp)

    状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...

  8. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  9. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

随机推荐

  1. ServletWeb缓存解决问题

    (1)为什么我们要防止这个问题的浏览器页面缓存: 所以在不须要缓存的页面中须要实现不缓存页面. 代码例如以下: package com.lc.HttpTest; import java.io.IOEx ...

  2. C#多线程编程实例 螺纹与窗口交互

    C#多线程编程实例 螺纹与窗口交互 代码: public partial class Form1 : Form { //声明线程数组 Thread[] workThreads = new Thread ...

  3. 我学cocos2d-x (两) 采用Delegate(信托)

    Delegate(信托)什么 Delegate是ios开发中的一个概念,主要是为了让类A中的功能,放到类B中来实现,这样能够合理的把功能划分到不同的文件里进行实现,从而更好的实现模块的分离.如UIAp ...

  4. 在最完整的搜索提示降史上的用户交互的研究——阅读《An Eye-tracking Study of User Interactions with Query Auto Completion》

            搜索下拉提示(Query Auto Completion,简称QAC)如今差点儿是每一个搜索引擎必备的基本功能,作用是在用户在搜索框输入查询词的过程中,给用户展示一系列搜索查询quer ...

  5. hdu4888 Redraw Beautiful Drawings

    14更多学校的第二个问题 网络流量   分别以行,列作为结点建图 i行表示的结点到j列表示的结点的流量便是(i, j)的值 跑遍最大流   若满流了便是有解   推断是否unique  就是在残余网络 ...

  6. 使用SQLServer 2008的CDC功能实现数据变更捕获

    原文:使用SQLServer 2008的CDC功能实现数据变更捕获 最近由于工作需要,研究了一下2008 CDC功能,觉得还不错,下面整理了一下研究过程,虽然比较粗略,但是基本上能用了,如果有补充请大 ...

  7. rabbitmq技术的一些感悟(一)

    Rabbitmq 初识rabbitmq RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQP(高级消息队列协议)的标准实现.假设不熟悉AMQP,直接看Rabbi ...

  8. 在win7在结构cocos2d-x v3.2rc0开发环境(For Android)

    cocos2d-x 这是现在比较流行的游戏引擎., 因此.本文的目的在于教导新手怎样在win7下建立cocos2dx开发环境, 截止本文,cocos2dx的最新版本号为 v3.2rc0版,我将如果您的 ...

  9. Android 墙纸设置代码 详细说明

    使游戏图像列表.思考添加壁纸功能.我发了一些资料. 1 别忘记在ApplicationManifest.xml 中加上权限的设置. <uses-permission android:name = ...

  10. 左右v$datafile和v$tempfile中间file#

    v$datafile关于存储在文件中的数据视图的信息,v$tempfile查看存储在一个临时文件中的信息. 有两种观点file#现场,首先来看看官方文件的定义: V$DATAFILE This vie ...