Back
Close
  • 50

Statement

The program:
You have to interpret a list of RPN (Reverse Polish Notation) instructions (from left to right).
You start by creating an empty stack and for each instruction:
- If the instruction is an integer, simply put in on top of the stack.
- The operations ADD, SUB, MUL, DIV, MOD pop the last two numbers out of the stack and push back on top the result of the corresponding operators (+,-,*,/,%). For example after, 2 5 SUB 8, the stack will contain -3 8.
- POP removes the top element of the stack.
- DUP duplicates the top element of the stack.
- SWP swaps the two top numbers of the stack.
- ROL pops the top element of the stack, then brings to the top the third element of the stack (from the top). For example, if the stack is 1 2 3 4, 5 ROL changes it to 1 3 4 2.

INPUT:
Line 1 : The number of instructions.
Line 2: All instructions separated by space.

OUTPUT:
You must print the stack separated by space.
If an instruction has fewer operands than needed or if we try to divide by 0, append ERROR to the stack, print it and stop the program.

CONSTRAINTS:
0 < N < 100

EXAMPLE:
Input
3
1 3 ADD
Output
4
 
Solve it

A higher resolution is required to access the IDE

Online Participants