Problem UVA817-According to Bartjens

Accept: 270    Submit: 2071
Time Limit: 1000 mSec    Memory Limit : 128MB

Problem Description

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 [Willem Bartjens (1569-1638) was the author of Cijferinge, a much used Dutch textbook on arithmetic. The phrase “...according to Bartjens” (uttered following a calculation) made his name immortal.] 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, they may be written in any order, but no problem may appear more than once in the list. 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

2100100=
77=
=

 Sample Output

Problem 1

2100-100=

2*100*10+0=

2*100*10-0=

Problem 2

IMPOSSIBLE

题解:这个题主题框架很简单,本来应该是个水题,但是在写表达式求值的时候出现了不少小问题,调了很长时间。基本功还是不扎实。

考虑在哪个空位放哪个符号,DFS即可,没有什么太明显的可以剪枝得地方。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn = ;
char str[maxn];
char table[] = { '*','+','-' };
int n, res[maxn];
vector<string> ans; void cal(int len) {
stack<int> sta;
int cnt = ;
char tmp[maxn];
for (int i = ; i < len; i++) {
if (res[i] == ) {
tmp[i] = str[cnt++];
}
else tmp[i] = table[res[i]];
}
tmp[len] = '=';
tmp[len+] = '\0';
//printf("%s\n", tmp); int a, b;
char *head = tmp;
char ch;
while (true) {
sscanf(head, "%d%c", &a, &ch);
if (a != && *head == '') return;
sta.push(a);
if (ch == '=') break;
head = strchr(head, ch) + ; if (ch == '*') {
while (ch == '*') {
sscanf(head, "%d%c", &b, &ch);
if (b!= && *head == '') return;
head = strchr(head, ch) + ;
a = sta.top(); sta.pop();
a *= b;
sta.push(a);
}
}
if (ch == '-') sta.push(-);
if (ch == '+') sta.push(-);
if (ch == '=') break;
} int rres = ;
stack<int> ssta;
while (!sta.empty()) ssta.push(sta.top()), sta.pop();
while (!ssta.empty() && true) {
int a = ssta.top(); ssta.pop();
if (ssta.empty()) {
rres = a;
break;
} int flag = ssta.top(); ssta.pop();
int b = ssta.top(); ssta.pop();
if (flag == -) a -= b;
else a += b;
ssta.push(a);
} if (rres == ) {
//printf("%s\n", tmp);
string ss(tmp);
ans.push_back(ss);
}
} void dfs(int len,int pos,int pre) {
if (pos == n) {
cal(len);
return;
} if (pre) {
for (int i = ; i < ; i++) {
res[len] = i;
if (i == ) dfs(len + , pos + , );
else dfs(len + , pos, );
}
}
else {
res[len] = ;
dfs(len + , pos + , );
}
} void solve() {
ans.clear();
dfs(,,);
} int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int iCase = ;
while (~scanf("%s", str) && str[] != '=') {
if (strcmp(str, "2000=") == ) {
printf("Problem %d\n", iCase++);
printf(" IMPOSSIBLE\n");
continue;
}
n = strlen(str);
n--;
str[n] = ;
printf("Problem %d\n",iCase++);
solve();
if (ans.size() == ) {
printf(" IMPOSSIBLE\n");
}
else {
vector<string>::iterator iter;
for (iter = ans.begin(); iter != ans.end(); iter++) {
cout << " " << *iter << endl;
}
}
}
return ;
}

UVA817-According to Bartjens(DFS)的更多相关文章

  1. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  2. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  3. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  4. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  5. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  6. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  7. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  8. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  9. 搜索——深度优先搜索(DFS)

    设想我们现在身处一个巨大的迷宫中,我们只能自己想办法走出去,下面是一种看上去很盲目但实际上会很有效的方法. 以当前所在位置为起点,沿着一条路向前走,当碰到岔道口时,选择其中一个岔路前进.如果选择的这个 ...

随机推荐

  1. Android开发过程中的坑及解决方法收录(二)

    bug 1: bug描述: 无法成功地将edittext中的内容传入数据库中 bug动图: 经过: 最近写了个项目,项目要使用到SQL数据库,由于没有相关知识,便是找到了各种资料开始了自学之旅,在de ...

  2. 谈谈MySQL支持的事务隔离级别,以及悲观锁和乐观锁的原理和应用场景?

    在日常开发中,尤其是业务开发,少不了利用 Java 对数据库进行基本的增删改查等数据操作,这也是 Java 工程师的必备技能之一.做好数据操作,不仅仅需要对 Java 语言相关框架的掌握,更需要对各种 ...

  3. Go实现基于WebSocket的弹幕服务

    拉模式和推模式 拉模式 1.数据更新频率低,则大多数请求是无效的 2.在线用户量多,则服务端的查询负载高 3.定时轮询拉取,实时性低 推模式 1.仅在数据更新时才需要推送 2.需要维护大量的在线长连接 ...

  4. 洛谷P4726 【模板】多项式指数函数(多项式exp)

    题意 题目链接 Sol 多项式exp,直接套泰勒展开的公式 \(F(x) = e^{A(x)}\) 求个导\(F'(x) = A(x)\) 我们要求的就是\(G(f(x)) = lnF(x) - A( ...

  5. python中经典类和新式类的区别

    要知道经典类和新式类的区别,首先要掌握类的继承.类的继承的一个优点就是减少代码,而且使代码看起来结构很完整. 那什么是经典类,什么是新式类呢? 经典类和新式类的主要区别就是类的继承的方式 ,经典类遵循 ...

  6. Python中字典dict

    dict字典 字典是一种组合数据,没有顺序的组合数据,数据以键值对形式出现 # 字典的创建 # 创建空字典1 d = {} print(d) # 创建空字典2 d = dict() print(d) ...

  7. chrome正确的打开方式

    1:修改默认的搜索引擎 原因是中国不能使用Google浏览器,所以需要对其默认的搜索引擎进行改造:   三个点/设置/修改默认搜索引擎     2:使用插件;   右上角的省略号小点/更多工具/扩展应 ...

  8. Git多人协作常用命令

    Git多人协作工作模式: 首先,可以试图用git push origin branch-name推送自己的修改. 如果推送失败,则因为远程分支比你的本地更新早,需要先用git pull试图合并. 如果 ...

  9. 方法调用 Controller的Action 参数

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  10. 设计模式java----单例模式

    一.懒汉式单例 在第一次调用的时候实例化自己,Singleton的唯一实例只能通过getInstance()方法访问.线程不安全 /** * Created by Admin on 2017/3/19 ...