Link:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=1641
Original title:
Background
Aroud A.D., EL Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had it origin in the financial Unts of a camel transaction. The formula lacked parenthesis and was ambiguous. So, him decided to ask Savants to provide him with a and to find which interpretation are the most advantageous for him, Depending on whether are is buying or selling the camels.
The Problem
You are are commissioned by El Mamum to write a program that determines the maximum and minimum possible of a P Arenthesis-less expression.
Input
The input consists of an integer n, followed by n lines, each containing a expression. Each expression is composed in most numbers, each ranging between 1 and separated by the sum and product ope Rators + and *.
Output
For each given expression, the output would echo a line with the corresponding maximal and minimal interpretations NG 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 bayi and.
The maximum and minimum are 1560 and 156.
The maximum and minimum are 339768 and 5023.
The main effect of the topic:
Give an expression without parentheses and only +, * Two operators, and then ask how to add parentheses to find the maximum and minimum value of the expression.
Analysis and Summary:
Require the maximum value, let + priority greater than *, required minimum value, let * priority is greater than +.
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
When calculating, use stack to do. Encounter a large priority operator, then pop two number to calculate, and then put the results into the stack, and finally, in the stack of all the number of the second priority of the operator to calculate, the result is the answer.
Code:
* * * Uva:10700-camel trading * result:accept * time:0.008s * author:d_dobule/#include <CSTDIO&G
T
#include <stack> using namespace std;
Stack<long long>min;
Stack<long long>max;
int main () {int T;
scanf ("%d", &t); while (t--) {while!
Min.empty ()) Min.pop (); while (!
Max.empty ()) Max.pop ();
Long long A, t;
Char ch;
scanf ("%lld", &a);
Min.push (a);
Max.push (a);
while ((Ch=getchar ())!= ' \ n ') {scanf ("%lld", &a);
if (ch== ' + ') {Min.push (a);
t = Max.top ();
Max.pop ();
T + A;
Max.push (t);
else if (ch== ' * ') {Max.push (a);
t = Min.top ();
Min.pop ();
T *= A;
Min.push (t);
} Long long ans_min=0, ans_max=1; while (!
Min.empty ()) {ans_min+=min.top ();
Min.pop (); while (!
Max.empty ()) {Ans_max *= max.top ();
Max.pop ();
printf ("The maximum and minimum are%lld and%lld.\n", ans_max,ans_min);
return 0; }
Author: csdn Blog shuangde800