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. 异常: Bean named 'org.springframework.transaction.interceptor.TransactionInterceptor#0' is expected to be of type 'org.aopalliance.aop.Advice' but was actually of type 'org.springframework.transaction.i

    场景: 在使用spring整合hibernate事务时报错解决: spring-aop中已经包含aopaliance,删除多余的jar包

  2. GET和POST两种基本请求方法的区别(转)

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...

  3. 通过css3实现的动画导航菜单代码

    用css3样式实现的滑动导航菜单,html代码如下 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" &quo ...

  4. BZOJ3028: 食物(生成函数)

    题意 链接 Sol 生成函数入门题. 对每个物品分别列一下,化到最后是\(\frac{x}{(1-x)^4}\) 根据广义二项式定理,最后答案是\(C_{(N - 1) + 4 - 1}^{4-1} ...

  5. git命令详解( 九 )

    此为git第九篇记录 整理提交记录 Git Cherry-pick     交互式的 rebase Git Tags  Git Describe 整理提交记录 之前我们已经学习了 Git 的基础知识 ...

  6. 华为P20无线投屏到电脑 绝地求生投射电脑

    如今出门在外,必不可少的就是手机,如果没有了手机,每个人都会感觉没有安全感,感觉和世界失去了联系,我们每天每个人都在使用手机,但是作为华为手机用户的你,了解华为P20无线投屏到电脑是怎么操作的吗? 使 ...

  7. C# 对象持久化

    本文以一个简单的小例子,简述对象持久化的相关知识,仅供学习分享使用,如有不足之处,还请指正. 概述 对象持久化是指将内存中的对象保存到可永久保存的存储设备中(如磁盘)的一种技术. 本文介绍的是除数据库 ...

  8. mumu模拟器安装xposed--如何在android模拟器上进行root

    问题描述 安装xposed表示failed to access root权限,新版的mumu模拟器没有了root选项,需要自己root. 1.先关掉应用兼容性,然后重启 电脑一般都是x86的,mumu ...

  9. (后端)sql手工注入语句&SQL手工注入大全(转)

    转自脚本之家: 看看下面的1.判断是否有注入;and 1=1;and 1=2 2.初步判断是否是mssql;and user>0 3.判断数据库系统;and (select count(*) f ...

  10. 页面间固定参数,通过cookie传值

    最后在做的页面,比如用户数据(用户头像,名称,年龄)这些信息,因为大部分页面都要用,之前是通过url地址传,另一页面接收.考虑到这样做会让url过长,因此,尝试使用cookie,把固定的值保存在coo ...