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 method to find which interpretation is the most advantageous for him, depending on whether is is buying or selling the camels.
You are commissioned by El Mamum to write a program that determines the maximum and minimum possible interpretation of a parenthesis-less expression.
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, and separated by the sum and product operators + and *.
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.
3 1+2*3*4+5 4*18+14+7*10 3+11+4*1*13*12*8+3*3+8
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#include #include #include #include #include #include #include #include #include