UVA817-According to Bartjens(DFS)
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
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)的更多相关文章
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 搜索——深度优先搜索(DFS)
设想我们现在身处一个巨大的迷宫中,我们只能自己想办法走出去,下面是一种看上去很盲目但实际上会很有效的方法. 以当前所在位置为起点,沿着一条路向前走,当碰到岔道口时,选择其中一个岔路前进.如果选择的这个 ...
随机推荐
- Elastic-Job-分布式调度解决方案
Elastic-Job是一个分布式调度解决方案,由两个相互独立的子项目Elastic-Job-Lite和Elastic-Job-Cloud组成. Elastic-Job-Lite定位为轻量级无中心化解 ...
- WarShall算法
1.引言 图的连通性问题是图论研究的重要问题之一,在实际中有着广泛的应用.例如在通信网络的联通问题中,运输路线的规划问题等等都涉及图的连通性.因此传递闭包的计算需要一个高效率的算法,一个著名的算法就是 ...
- python 实现微信自动回复(自动聊天)
原文地址(本人):https://blog.csdn.net/a5878989/article/details/54974249 介绍 微信自动回复其实主要就是登录,接收消息,回复消息三个功能,微信没 ...
- 减少页面加载时间的n种方法
减少HTTP请求 1:减少调用其他页面.文件的数量 2:使用css spirit , 减少图片加载次数 压缩js,css代码 1:一般js.css文件中存在大量的空格.换行.注释,这些利于阅读,如果能 ...
- 一个优秀的SEOer必须掌握的三大标配技术
首先,认识网页代码是基础 这里所讲的网页代码是指HTML代码,并不是指复杂的PHP模板技术.一般的培训机构总是提倡学SEO不用学网页代码,只要会购买域名空间搭建网站就行,因为现在的网站模板太丰富了,对 ...
- java map集合 --遍历
1.Map 遍历: Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "a& ...
- Glide图片加载框架小bug
如上一段加载图片的代码,本身是没问题的,后来测试发现有情况不显示url对应的图片,而一直显示加载超时的图片 修改如下: 将with()方法的上下文context改为图片的imageView.getCo ...
- (网页)a标签下载
HTML <a> download 属性 <a href="/images/myw3schoolimage.jpg" download="w3logo& ...
- Bresenham算法的实现思路
条件已知两个点的坐标p1(x0,y0),p2(x1,y1)要求画出这条直线 之后的e代表每次的误差积累,初始值为0,可以计算出斜率为k=dy/dx=(y0-y1)/(x0-x1) 1.x为阶跃步长(直 ...
- python异常处理与断言以及日志模块
python异常处理与断言 目录: 1.异常处理 2.断言(assert) 3.日志模块(logging) 4.修改之前的车票信息查询,把日志模块.异常处理加进去 1.异常处理 代码如下: 语法: t ...