"Programming Marathon algorithm Directory"
"018-Don't like brackets" "Engineering download >>>"
1 Topic Description
Nowcoder like maths from an early age and likes to record many expressions in his notes. It feels that now the expression is very troublesome, in order to improve the operator priority, have to add a lot of parentheses, accidentally missing a right parenthesis horseshoes.
Instead, he uses prefix expressions, such as writing (2 + 3) * 4
* + 2 3 4
, to avoid using parentheses. Such expressions are simple to write, but not intuitive to calculate. Please write a program to help him calculate these prefix expressions.
1.1 Input Description:
The input contains multiple sets of data, each containing two rows.
The first behavior is a positive integer n (3≤n≤50), followed by the second row containing n a list of numbers and operators.
"+-*/" is subtraction arithmetic, where division is divided evenly, that is, "5/3=1".
1.2 Output Description:
corresponding to each set of data, output their operation results.
1.3 Input Example:
3+ 2 35* + 2 2 35* 2 + 2 3
1.4 Output Example:
51210
2 ideas for solving problems
Because the input is a prefix (also called inverse polish), you can use a stack to store the input operand. The operands are processed from the end of the input sequence , and when the operand is encountered, the operand is stored in the stack, and when the operator is encountered, two operands are popped from the stack, and the result is stored again in the stack. Loop the above operation until all the content is processed, and the last stack has only one element, which is the result of the request.
3 Algorithm Implementation
ImportJava.util.ArrayDeque;ImportJava.util.Deque;ImportJava.util.Scanner;/** * Author: Wang Junshu * time:2016-05-12 08:41 * CSDN:HTTP://BLOG.CSDN.NET/DERRANTCM * github:https://github.com/wang-ju N-chao * declaration:all rights Reserved!!! */ Public class Main { Public Static void Main(string[] args) {Scanner Scanner =NewScanner (system.in);//Scanner Scanner = new Scanner (Main.class.getClassLoader (). getResourceAsStream ("Data.txt")); while(Scanner.hasnext ()) {intnum = Scanner.nextint (); string[] suffix =NewString[num]; for(inti =0; i < num; i++) {Suffix[i] = Scanner.next (); } System.out.println (Calculate (suffix)); } scanner.close (); }/** * Calculate prefix type * * @param suffix prefix * @return results * / Private Static int Calculate(string[] suffix) {Deque<integer> stack =NewArraydeque<> (); for(inti = suffix.length-1; I >=0; i--) {Charc = Suffix[i].charat (0);//If it is an operator if(suffix[i].length () = =1&& (c = =' + '|| c = ='-'|| c = =' * '|| c = ='/')) {intA = Stack.removefirst ();intb = Stack.removefirst (); Stack.addfirst (Calculate (A, B, c)); }//operand in stack Else{Stack.addfirst (Integer.parseint (Suffix[i])); } }returnStack.removefirst (); }/** * Calculation of ACB * * @param a operand * @param b operand * @param c operator * @r Eturn Results * / Private Static int Calculate(intAintBCharc) {Switch(c) { Case ' + ':returnA + b; Case '-':returnA-B; Case ' * ':returnA * b; Case '/':returnA/b;default:// do nothing}Throw NewIllegalArgumentException ("operator can only be (+-*/):"+ c); }}
4 Test Results
5 Other information
Because Markddow is not good for editing, uploading a picture of a document for reading. PDF and Word documents can be "downloaded >>>" on GitHub.
"Programming Marathon" "018-don't like parentheses"