UVA 10700 Camel trading
2015-02-18 13:50
537 查看
Camel trading
Time Limit: 1 second
Background
Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had its origin in the financial accounts of a camel transaction. The formula lacked parenthesis and was ambiguous. So, he decided to ask savants to provide him with a methodto find which interpretation is the most advantageous for him, depending on whether is is buying or selling the camels.
The Problem
You are commissioned by El Mamum to write a program that determines the maximum and minimum possible interpretation of a parenthesis-less expression.
Input
The input consists of an integer N, followed by N lines, each containing an expression. Each expression is composed of at most 12numbers, each ranging between 1 and 20, andseparated by the sum and product operators + and *.
Output
For each given expression, the output will echo a line with the corresponding maximal and minimal interpretations, following the format given in the sample output.
Sample input
3 1+2*3*4+5 4*18+14+7*10 3+11+4*1*13*12*8+3*3+8
Sample output
The maximum and minimum are 81 and 30. The maximum and minimum are 1560 and 156. The maximum and minimum are 339768 and 5023.
可以加括号,求表达式的最大最小值。
因为只有乘法和加法,所以最小值就是什么都不做,最大值是先算加法。
到了使用栈的时候了,c++真是方便啊。
#include<iostream> #include<algorithm> #include<cmath> #include<cstdio> #include<string> #include<cstring> #include<cstdlib> #include<queue> #include<vector> #include<functional> #include<stack> using namespace std; long long t, sum1, sum2, c, d; char ch; stack<long long> a, b; int main(){ cin >> t; while (t--) { scanf("%lld", &c); a.push(c); b.push(c); while (scanf("%c", &ch) && (ch == '+' || ch == '*')) { scanf("%lld", &c); if (ch == '+'){ d = a.top() + c; a.pop(); a.push(d); b.push(c); } else{ d = b.top() * c; b.pop(); b.push(d); a.push(c); } } sum1 = 1; while (!a.empty()) sum1 *= a.top(), a.pop(); sum2 = 0; while (!b.empty()) sum2 += b.top(), b.pop(); printf("The maximum and minimum are %lld and %lld.\n", sum1, sum2); } return 0; }
相关文章推荐
- uva 10700 - Camel trading
- UVA 10700 Camel trading 贪心(栈的使用)
- UVa 10700 - Camel trading
- UVA 10700 Camel trading
- 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
- uva10700 Camel trading
- UVa 10700 Camel trading
- UVA 10700 Camel trading 无括号的表达式 贪心
- UVA - 10700 Camel trading
- UVA - 10700 Camel trading