#K11861. Evaluate Prefix Expression
Evaluate Prefix Expression
Evaluate Prefix Expression
You are given an arithmetic expression in prefix (Polish) notation. The expression consists of integers and the operators +
, -
, *
, and /
. Your task is to evaluate the expression and output the result as an integer.
The evaluation should be performed in the following manner: traverse the expression from right to left, and when an operator is encountered, apply it to the next two operands on the stack. For division, use integer division that truncates towards zero. In mathematical terms, for operands \(a\) and \(b\) with the operator \(\div\), compute \(\lfloor a/b \rfloor\) if both \(a\) and \(b\) are positive, and generally perform truncation toward zero.
inputFormat
The input is given in a single line from the standard input. It contains the tokens of the prefix expression separated by spaces.
For example: + 3 4
outputFormat
Output the evaluated integer result to standard output.
For example: 7
+ 3 4
7