题意:给出只包含数字和+*的表达式,你可以自己安排每一个运算的顺序,让你找出表达式可能得到的最大值和最小值。
很明显,先乘后加是最小值,先加后乘能得到最大值。
其实不是很明显。。。
证明下:
数字的范围是大于等于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); while (n--) { m = -1; mc(o); mc(s); while (o[m++] != ' ') scanf("%lld%c", &s[m], &o[m]); printf("The maximum and minimum are %lld and %lld. ", Max(), Min()); } return 0; }