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. es6 语法 (Promise)

    { // 基本定义 let ajax = function(callback) { console.log('执行'); //先输出 1 执行 setTimeout(function() { call ...

  2. vue从入门到进阶:简介(一)

    前言 用了这么久的vue了,但是一直没有时间写个系列文章,现在抽一定时间总结下vue的知识点. 首先,Vue 不支持 IE8 及以下版本,因为 Vue 使用了 IE8 无法模拟的 ECMAScript ...

  3. router-view在vue2.0中不显示,解决方法

    学习的router-view路由一直不显示,我翻看vue文档,花费1个小事终于找到原因,希望我的问题能给初学的同学,带来福音,版本不一样,真的烦 在此总结记录,以免以后在此遇到此问题 //配置路由 c ...

  4. 如何为你的 Vue 项目添加配置 Stylelint

    如何为你的 Vue 项目添加配置 Stylelint 现在已经是 9102 年了,网上许多教程和分享帖都已经过期,照着他们的步骤来会踩一些坑,如 stylelint-processor-html 已经 ...

  5. 开源项目商业模式分析(2) - 持续维护的重要性 - Selenium和WatiN

    该系列第一篇发布后收到不少反馈,包括: 第一篇里说的MonicaHQ不一定盈利 没错,但是问题在于绝大多数开源项目商业数据并没有公开,从而无法判断其具体是否盈利.难得MonicaHQ是公开的,所以才用 ...

  6. 实现加载Tomcat服务器中的图片,并且有进度条提示进度

    首先布局页面, <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...

  7. 微信小程序开发之初探

    本文是以一个简单的小例子,来简要讲解微信小程序开发步骤,希望促进学习分享. 概念 微信小程序,简称小程序,缩写xcx,英文mini program.是一种不需要下载安装即可使用的应用,它实现了应用“触 ...

  8. (后台)SQL Server 数据库定时自动备份(转)

    转自博客园: SQL Server 数据库定时自动备份[转]   在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库.而备份数据库一般又是在凌晨时间基本没有数据库操作的时候进行,所以 ...

  9. g4e基础篇#5 创建分支和保存代码

    章节目录 前言 1. 基础篇: 为什么要使用版本控制系统 Git 分布式版本控制系统的优势 Git 安装和设置 了解Git存储库(Repo) 起步 1 – 创建分支和保存代码 起步 2 – 了解Git ...

  10. 前端AntD框架的upload组件上传图片时遇到的一些坑

    前言 本次做后台管理系统,采用的是 AntD 框架.涉及到图片的上传,用的是AntD的 upload 组件. 前端做文件上传这个功能,是很有技术难度的.既然框架给我们提供好了,那就直接用呗.结果用的时 ...