Ultraviolet A 10700-camel trading

Source: Internet
Author: User

Link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 113 & page = show_problem & problem = 1641


Original question:

Background

Aroud 800. 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 method to find which interpretation is the most advantageous for him, depending on whether 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 integerN, FollowedNLines, each containing an expression. Each expression is composed of at most12Numbers, each ranging
Between1And20, 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

31+2*3*4+54*18+14+7*103+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.

Question:

Add parentheses to an expression that contains only the plus (+) and minus (*) operators. Then, add parentheses to obtain the maximum and minimum values of the expression.


Analysis and Summary:

If the maximum value is required, the priority of + is greater than *. If the minimum value is required, the priority of * is greater than +.

Stack is used for computing. When the operator with a higher priority is met, two numbers are displayed for calculation, and the result is pushed into the stack. Finally, all the numbers in the stack are calculated using the second-priority operator. The result is the answer.

Code:

/* *  UVa:  10700 - Camel trading *  Result: Accept *  Time: 0.008s *  Author: D_Dobule */#include<cstdio>#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;}

 
  

-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 , By d_double (reprinted, please mark)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.