UVA - 10700 Camel trading(贪心+栈)
2014-09-14 20:27
316 查看
Problem E - 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, hedecided to ask savants to provide him with a method to 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 12 numbers, each ranging between 1 and20,and separated 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.
题目大意:
给你一个表达式,这个表达式只有*和+,现在要你可以随便加括号,算出这个加上括号的最大值和最小值。
解析:贪心题,如果求得是最大值就先算+,如果是最小值就先算*。
最大值栈中的所有的值相加,最小值栈中的所有的值相乘。
#include <cstdio> #include <cstring> #include <stack> using namespace std; typedef long long ll; int main() { ll num,tmp; char ch; int t; scanf("%d",&t); while(t--) { stack<ll> Max; stack<ll> Min; scanf("%lld",&num); Max.push(num); Min.push(num); while((ch = getchar()) != '\n') { scanf("%lld",&num); if(ch == '+') { Min.push(num); tmp = Max.top(); Max.pop(); tmp += num; Max.push(tmp); }else if(ch == '*'){ Max.push(num); tmp = Min.top(); Min.pop(); tmp *= num; Min.push(tmp); } } ll max,min; max = 1 , min = 0; while(!Max.empty()) { max *= Max.top(); Max.pop(); } while(!Min.empty()) { min += Min.top(); Min.pop(); } printf("The maximum and minimum are %lld and %lld.\n",max,min); } 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 (模拟栈)
- uva10700 - Camel trading(无括号的表达式)
- UVa 10700 Camel trading(计算式最大和最小值)
- Camel trading - UVa 10700 dp
- uva:10700 - Camel trading(贪婪)
- UVA 10700-Camel trading(栈求表达式的最大最小值)
- uva 10700 Camel trading(STL 栈)
- uva 10700 Camel trading (水贪心+栈)
- Wine trading in Gergovia UVA - 11054 贪心思维
- UVa 11054 Wine trading in Gergovia【贪心】
- UVA 11054 Wine trading in Gergovia(贪心)
- Wine trading in Gergovia uva+贪心
- UVA-11054 Wine trading in Gergovia(贪心)
- uva - 11054 - Wine trading in Gergovia(贪心)