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. python之把字符串形式的函数编译执行

    实现效果:执行字符串形式的函数 代码如下 # name = 'aaa' # data = [18,32,33] # def hellocute(): # return "name %s ,a ...

  2. vue+vuecli+webpack中使用mockjs模拟后端数据

    前言 使用mockjs可以事先模拟数据,前提是和后端约定好了数据接口,怎样的数据.使用mock就可以生成你要的数据了,从而实现开发时前后端分离. 其主要功能是: 基于数据模板生成模拟数据. 基于HTM ...

  3. Salesforce 的 package.xml 文件

    package.xml文件 在部署元数据(Metadata)的时候,package.xml是很关键的一个文件.此文件中定义了一个XML格式的列表,其中包含了各个元数据组件的定义. Metadata A ...

  4. 想让安卓app不再卡顿?看这篇文章就够了

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由likunhuang发表于云+社区专栏 实现背景 应用的使用流畅度,是衡量用户体验的重要标准之一.Android 由于机型配置和系统的 ...

  5. MFC更换画笔(画刷)颜色以及画眼睛(GDI画图)

    MFC画眼睛 换画笔(画刷)颜色(参考链接:https://blog.csdn.net/sunxiving/article/details/51272001) 由于画笔一旦创建后就无法修改.所以要修改 ...

  6. python如何实现类似php的引用赋值

    直接放代码,有注释,就不解析了 # ############################ # 改变一个变量的值,与之有关系的变量的值也会相等变化 # 类似php的引用赋值,宏观来看 # ##### ...

  7. Android Room框架学习笔记

    一.使用 1.build.gradle引入 compile "android.arch.persistence.room:runtime:1.0.0" annotationProc ...

  8. SQL Server 锁实验(UPDATE加锁探究)

    update语句: 本例中由于看到的是update执行完的锁情况,因此无法看到IU锁,但其实针对要修改的数据页和索引页会先加IU锁,记录和键先加U锁,然后再转化为IX和X锁. 如果想要看到IU锁和U锁 ...

  9. 简单易懂的程序语言入门小册子(1.5):基于文本替换的解释器,递归定义与lambda演算的一些额外说明

    这一篇接在第一篇lambda演算的后面.讲讲一些数学知识. 经常有些看似很容易理解的东西,一旦要描述得准确无误,就会变得极为麻烦. 软件工程里也有类似情况:20%的代码实现了核心功能,剩下80%的代码 ...

  10. Linux学习历程——Centos 7重置root密码

    一.自述 最近刚刚接触linux,因为我设置密码比较随性,把自己做系统的时候设置的root密码给forget,每当系统崩溃,重新把虚拟机备份还原后,就面临无法登陆的尴尬情况,只得重置root密码,好了 ...