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

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

其实不是很明显。。。

证明下:

数字的范围是大于等于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. .NET面试宝典-高级(一)

    1. DateTime.Parse(myString); 这段代码有什么问题? A:区域信息即CultureInfo没有指定.如果不指定的话,它将采用默认的机器级的设置(见:控制面板->区域和语 ...

  2. ROS知识(8)----CMakeLists.txt文件编写的理解

    ROS(Indigo)编程必须要理解CMakeList.txt的编写规则,教程地址:catkin/CMakeLists.txt,官网有相关的教程,中文的翻译版本写的很不错,教程地址:ROS中的CMak ...

  3. 配置主从Mysql

    怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作:  1.1.版本一致  1.2.初始化表,并在后台启动mysql  1.3.修改root的密码 2.修 ...

  4. CentOS 7 下编译安装lnmp之nginx篇详解

    一.安装环境 宿主机=> win7,虚拟机 centos => 系统版本:CentOS Linux release 7.5.1804 (Core),ip地址 192.168.1.168   ...

  5. 华为S5300系列交换机V100R006SPH019升级补丁

    S5300_V100R006SPH019.pat 附件: 链接:https://pan.baidu.com/s/1M1S5amGGViUieSp8lJ9psw  密码:sexx

  6. golang 字符串与整数, 布尔转换 strconv

    strconv 是golang对于字符串和基本数据类型之间的转换字符串转整数testStr := "1000" testInt, err := strconv.Atoi(testS ...

  7. error MSB8031: Building an MFC project for a non-Unicode character set is deprecated

    vs2013编译VC++源码,错误: error MSB8031: Building an MFC project for a non-Unicode character set is depreca ...

  8. 【spring cloud】spring cloud集成zipkin报错:Prometheus requires that all meters with the same name have the same set of tag keys.

    spring boot 2.0.X 的版本,整合zipkin2.10.1 zipkin服务启动后,访问zipkin的UI http://localhost:8002/zipkin/ 页面显示空白,cs ...

  9. Tomcat集群Spring+Quartz多次执行解决方案记录

    由于在集群环境下定时器会出现并发和重复执行的问题,我再三考虑记录有5 一.把定时器模块单独拿出来放到一台tomcat或者新建一个Java工程手动启动定时器,这样定时器的任务就可以从原来的集群中抽离开来 ...

  10. POP按钮动画

    POP按钮动画 效果 源码 https://github.com/YouXianMing/Animations // // ButtonPressViewController.m // Faceboo ...