#K16191. Evaluate Polish Notation
Evaluate Polish Notation
Evaluate Polish Notation
This problem requires you to evaluate a mathematical expression written in prefix (Polish) notation. In prefix notation, every operator precedes its operands. For example, the expression \( +\ 2\ 3 \) is equivalent to \(2 + 3 = 5\).
You are given a sequence of tokens representing a valid prefix expression that may include the binary operators +
, -
, *
, and /
as well as integer operands. Note that division should be performed as integer division. Your task is to compute and output the result of the expression.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains an integer \( n \), which represents the number of tokens in the expression.
- The second line contains \( n \) space-separated tokens. Each token is either an operator (one of
+
,-
,*
,/
) or an integer operand.
You may assume that the given expression is valid and every operator has exactly two operands.
outputFormat
Output to stdout a single integer: the value obtained by evaluating the prefix expression.
## sample3
+ 2 3
5
</p>