#C377. Evaluate Reverse Polish Notation Expression
Evaluate Reverse Polish Notation Expression
Evaluate Reverse Polish Notation Expression
Given an arithmetic expression in Reverse Polish Notation (RPN), evaluate the result using a stack-based algorithm.
The input is a single line string containing tokens separated by spaces. Each token is either an integer or one of the operators +
, -
, *
, and /
. The division operation should truncate toward zero (i.e., perform integer division). Formally, if \(a\) and \(b\) are integers and \(b \neq 0\), then
$$
\frac{a}{b}=\operatorname{trunc}\left(\frac{a}{b}\right).
$$
It is guaranteed that the given RPN expression is valid and will evaluate to a single integer result.
inputFormat
The input consists of a single line containing a valid RPN expression, where tokens are separated by spaces.
Example: 2 1 + 3 *
outputFormat
Output a single integer which is the result of evaluating the RPN expression.
## sample2 1 + 3 *
9
</p>