Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

THANOS vs Physics Lab

By PKP007 · Limits 1s, 512 MB

After conquering 6 infinity stones THANOS found out that there is another stone called Backlog Stone which is more powerful than any other stone. So after searching the whole universe for the stone, THANOS came to know that the stone is hidden in the Dark Room of Physics Lab.

So he went to Physics lab for conquering that stone. But in front of the gate of Physics Lab, Dokaziz the Protector of Backlog Stone was standing. No one could ever get past him and enter into Physics Lab. Because if anybody wants to pass the gate, Dokaziz gives him/her a mathematical expression prepared by Euclid Lagrange Archimedes Laplace aka E_LAL the God of Mathematics to solve. Though THANOS is the strongest super villain of all time, he is very weak in mathematics. He passed all of his math courses in student life just by memorizing some CHOTHA.

But THANOS is very clever. Somehow he got informed about RUET GyanJam 2019. So, he captured Pikada (one of the problem setters of this contest) and forced him to set a problem for solving mathematical expression. THANOS knows contestants of RUET GyanJam 2019 are very intelligent and some of them will surely solve that problem. With that Accepted code, THANOS can solve any mathematical expression and surpass Dokaziz and then finally conquer the highly desired Backlog Stone.

So this is the problem Pikada made for THANOS. But Pikada is in danger now. If none can solve this problem THANOS will snap his fingers and turn half of Pikada into ashes. So Pikada is counting on you. Solve this problem and save Pikada from THANOS.

In this problem, you will be given some mathematical expressions and you just have to tell the final results of those expressions.
In mathematics, an expression or mathematical expression is a finite combination of symbols that is well-formed according to rules that depend on the context. Mathematical symbols can designate numbers (constants), variables, operations, functions, brackets, punctuation, and grouping to help determine order of operations, and other aspects of logical syntax.

For example: 2+5-3 is a mathematical expression and its result is 4.

For this problem a mathematical expression will only contain numbers and four operators + (Plus), - (Minus), * (Multiplication) and / (Division).
You have to follow the precedence order for solving a mathematical expression, as listed below.

  1. Division
  2. Multiplication
  3. Addition/Subtraction

That means you have to deal with all division first, then multiplication and then addition/subtraction.

In this problem, a mathematical expression will be a string consisting of only digits (0-9) and four operators (+, -, *, /). It is guaranteed that every given string will be a valid mathematical expression, and every number in the expression and the result will fit in 32-bit integer.

[Note: Use integer division for division operation. That means a/b = floor(a/b). And treat all the operators as left associative.]


The first line contains an integer T (T ≤ 10), the number of expressions.
Each of the next T lines contains a mathematical expression. A mathematical expression is a string that contains only the digits [0-9] and (+, -, *, /) symbols.

The length of a mathematical expression will be 100 at max and the expressions are guaranteed to be valid mathematical expressions, i.e. the the input is such that you will never need to divide by 0 to evaluate the expression.


For each test case, output a line like ‘Case X: Y’ (without the quotation) where X denotes the number of test case and Y denotes the result of the mathematical expression.

See sample I/O for better understanding


Case 1: 16
Case 2: 250
Case 3: 15



    53% Solution Ratio

    NirjhorEarliest, Aug '19

    NirjhorFastest, 0.0s

    Hidden191Lightest, 0 B

    sarthakmannaShortest, 470B


    Login to submit


    Category: Ad-hoc, Infix to Postfix conversion

    Toph uses cookies. By continuing you agree to our Cookie Policy.