UVA 10700 Camel trading 无括号的表达式 贪心
来源:互联网 发布:sift 算法编程原理 编辑:程序博客网 时间:2024/06/05 14:07
题意:给出只包含数字和+*的表达式,你可以自己安排每一个运算的顺序,让你找出表达式可能得到的最大值和最小值。
很明显,先乘后加是最小值,先加后乘能得到最大值。
其实不是很明显。。。
证明下:
数字的范围是大于等于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 无括号的表达式 贪心
- uva10700 - Camel trading(无括号的表达式)
- UVA 10700 Camel trading(贪心)
- UVA 10700 Camel trading 贪心(栈的使用)
- uva - 10700 - Camel trading(贪心)
- uva:10700 - Camel trading(贪心)
- UVA - 10700 Camel trading(贪心+栈)
- UVA - 10700 - Camel trading (简单贪心)
- UVA 10700-Camel trading(栈求表达式的最大最小值)
- UVa 10700 Camel trading (计算表达式)
- uva 10700 Camel trading
- UVa 10700 - Camel trading
- uva 10700 - Camel trading
- uva 10700 - Camel trading
- UVA 10700 Camel trading
- UVA 10700 - Camel trading
- UVa 10700 Camel trading
- uva 10700 Camel trading
- 吃论扯谈---吃饭和Office 365订阅的关系
- ZSKAME大白菜2013官网下载
- 用typedef声明类型
- 华为上机笔试题目
- android的GridView显示网络图片的方法 .
- UVA 10700 Camel trading 无括号的表达式 贪心
- NCBI SRA数据预处理
- C#中使用设置介绍
- HDU 4642 (13.08.25)
- 【专题】Catalan数
- Hadoop集群配置(最全面总结)
- android GridView 通过json显示网络图片和文字
- Linux Commands for Beginners--Changing Ownership --chown command
- exit 与 return 的区别