题意:给出只包含数字和+*的表达式,你可以自己安排每一个运算的顺序,让你找出表达式可能得到的最大值和最小值。

很明显,先乘后加是最小值,先加后乘能得到最大值。

其实不是很明显。。。

证明下:

数字的范围是大于等于1的,所以a+b*c如果先考虑加法就变成(a+b)*c,变换下就是ac+bc,而先考虑乘法的话就是a+bc,由于c是大于等于1的,所以ac+bc>=a+bc,先考虑加法结果会不小于先考虑乘法。

根据这个思想,先乘后加是最小值,先加后乘能得到最大值。

代码:

 /*
* Author: illuz <iilluzen@gmail.com>
* Blog: http://blog.csdn.net/hcbbt
* File: uva10700.cpp
* Lauguage: C/C++
* Create Date: 2013-08-25 16:58:52
* Descripton: UVA 10700 Camel trading, simulation
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <utility>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repu(i, a, b) for (int i = (a); i < (b); i++)
#define repf(i, a, b) for (int i = (a); i <= (b); i++)
#define repd(i, a, b) for (int i = (a); i >= (b); i--)
#define swap(a, b) {int t = a; a = b; b = t;}
#define mc(a) memset(a, 0, sizeof(a))
#define ms(a, i) memset(a, i, sizeof(a))
#define sqr(x) ((x) * (x))
#define FI(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
typedef long long LL;
typedef unsigned long long ULL; /****** TEMPLATE ENDS ******/ const int MAXN = 30;
char o[MAXN];
LL s[MAXN], m; LL Min() {
LL sum = 0;
rep(i, m) {
LL t = s[i];
while(o[i] == '*') t *= s[++i];
sum += t;
}
return sum;
} LL Max() {
LL sum = 1;
rep(i, m) {
LL t = s[i];
while(o[i] == '+') t += s[++i];
sum *= t;
}
return sum;
} int main() {
int n;
scanf("%d\n", &n);
while (n--) {
m = -1;
mc(o); mc(s);
while (o[m++] != '\n')
scanf("%lld%c", &s[m], &o[m]);
printf("The maximum and minimum are %lld and %lld.\n", Max(), Min());
}
return 0;
}

UVA 10700 Camel trading 无括号的表达式 贪心的更多相关文章

  1. UVa 10700 - Camel trading

    题目大意:给一个不含括号.只有+和*运算的表达式,数字的范围在1到20之间,算出计算结果的可能最大值和最小值. 贪心,如果加法优先级比乘法高,那么得出的结果为最大值.(a+b)*c = a*c + b ...

  2. uva:10700 - Camel trading(贪婪)

    题目:10700 - Camel trading 题目大意:给出一些表达式,表达式由数字和加号乘号组成,数字范围[1,20].这些表达式可能缺少了括号,问这种表达式加上括号后能得到的最大值和最小值. ...

  3. UVA 11054 Wine trading in Gergovia 葡萄酒交易 贪心+模拟

    题意:一题街道上很多酒店,交易葡萄酒,正数为卖出葡萄酒,负数为需要葡萄酒,总需求量和总售出量是相等的,从一家店到另外一家店需要路费(路费=距离×运算量),假设每家店线性排列且相邻两店之间距离都是1,求 ...

  4. c++对象创建带括号与无括号的区别

    class Test{public: Test() {} Test(int a) {}} 1.栈上创建对象 1.1 无括号 Test a; // 调用默认构造函数,栈上分配内存创建对象 1.2 有括号 ...

  5. UVA10700:Camel trading(栈和队列)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/J 题目大意: 给一个没有加上括号的表达式且只有+ , ...

  6. UVA LA 7146 2014上海亚洲赛(贪心)

    option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosm ...

  7. uva 11054 wine trading in gergovia (归纳【好吧这是我自己起的名字】)——yhx

    As you may know from the comic \Asterix and the Chieftain's Shield", Gergovia consists of one s ...

  8. UVA 1626 Brackets sequence(括号匹配 + 区间DP)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/E 题意:添加最少的括号,让每个括号都能匹配并输出 分析:dp ...

  9. [题解]UVA10700 Camel trading

    链接:http://vjudge.net/problem/viewProblem.action?id=21358 描述:给出一个算式,算式里面有加法和乘法,可以任意添加括号从而改变计算顺序.求可能得到 ...

随机推荐

  1. UVALive 4863 Balloons 贪心/费用流

    There will be several test cases in the input. Each test case will begin with a line with three inte ...

  2. cookie、sessionStorage、localStorage 详解

    转自--http://www.cnblogs.com/fly_dragon/p/3946012.html cookie Cookie的大小.格式.存储数据格式等限制,网站应用如果想在浏览器端存储用户的 ...

  3. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  4. 读书笔记_Effective_C++_条款三十八:通过复合塑模出has-a或者is-implemented-in-terms-of

    如果说public是一种is-a的关系的话,那么复合就是has-a的关系.直观来说,复合就是在一个类中采用其他类的对象作为自身的成员变量,可以举个例子,像下面这样: class Person { pr ...

  5. UIAutomator2.0初始

    1. 先直接上样例,谷歌官方Demo: https://github.com/googlesamples/android-testing 2. 一句话说明改动思路 Most importantly, ...

  6. PHP常量定义之define与const对比

    简要归纳PHP中两个常量定义关键字的区别: 1.define是函数,const是语言结构,PHP编译时const速度快.2.define只能用在类外,const类内类外皆可.3.define定义的常量 ...

  7. Detecting Underlying Linux Distro

    If you are the owner of the system, then you know which Linux is installed and running. This article ...

  8. HTML5中的Web Storage(sessionStorage||localStorage)理解与简单实例

    Web Storage是什么? Web Storage功能,顾名思义,就是在Web上针对client本地储存数据的功能,详细来说Web Storage分为两种: sessionStorage: 将数据 ...

  9. Android之判断时间是否为今天

    字符串:      sdate =  2013-07-16 13:35:02 /** * 判断给定字符串时间是否为今日 * @param sdate * @return boolean */ publ ...

  10. 机器学习实战:用nodejs实现人脸识别

    机器学习实战:用nodejs实现人脸识别   在本文中,我将向你展示如何使用face-recognition.js执行可靠的人脸检测和识别 . 我曾经试图找一个能够精确识别人脸的Node.js库,但是 ...