ARITH2 - Simple Arithmetics II

While browsing aimlessly, Peter stumbled upon an old riddle he used to solve on his calculator when he was still young. It was the kind of a riddle where you punch in a bunch of numbers and operators into a simple pocket calculator and then turn it upside down to get the answer:

These come in many different sizes but they are always exactly one foot long. Answer: 103 * 103 * 5.

What are made of ice to keep people warm? Answer: 50 * 40 * 250 + 791.

After a few minutes he found a large amount of such riddles and full of excitement he went to solve them. He turned his computer screen upside down...

... only to find out that he does not have a reasonable calculator program installed on his computer.

Problem specification

You are given multiple sequences of button presses of a simple pocket calculator that consist of digits and arithmetic operators. For each such sequence find the number it would produce on a pocket calculator's display.

Note that the pocket calculator evaluates the operators in the order in which they are given. (i.e., there is no operator precedence.) Assume that the display of the calculator is large enough to show the result, and that its memory is sufficient to store all intermediate results.

Input specification

The first line of the input file contains an integer T specifying the number of test cases. Each test case is preceded by a blank line.

Each test case represents one sequence of button presses for a pocket calculator. The sequence consists of non-negative integers and arithmetic operators and ends with an equal sign. It may also contain spaces to improve readability.

The operator / represents integer division, rounded down. You may assume that no test case contains division by zero and that in all test cases all intermediate results are non-negative.

Tip: long long int in C/C++, long in Java or int64 in Pascal is enough for this problem.

Output specification

For each sequence from the input file output the number that would be displayed on the calculator.



1 + 1 * 2 =

29 / 5 =

103 * 103 * 5 =

50 * 40 * 250 + 791 =



The first test case shows that there is no operator precedence.

The second one shows that integer division always rounds down.

The last two outputs are the answers to the two riddles in the problem statement: "shoes" (53045 upside down), and "igloos"(500791 upside down).

hide comments
Manish Das Mohapatra: 2015-08-21 04:48:08

Finally AC after 4 attemps.
No switch, No array, No strings.
Careful abt the spaces in between.

prakash_reddy: 2015-08-12 14:45:30

Good one.... :)

Harsh Vardhan Ladha: 2015-08-12 06:57:38

Did with Python's eval function :D

MishThi: 2015-08-05 06:49:54

Watch out for blank lines in the input.. It cost me 1 Runtime NZEC in Python.. :P

Kshitij Garg: 2015-06-15 16:37:08

The question setter should seriously look into improving the test cases .
Even though it is recommended to use LONG LONG , I got AC with INT only .

akar sheth: 2015-06-03 18:25:09

The blank lines gave me 1 WA.......... (;

Rudra Nil Basu: 2015-05-01 15:59:49

AC at first go :)
Easy one.
Hint - Read the problem statement (and the sample example) carefully and no need to store in any array.

arjun: 2015-04-15 04:15:21

my 25th. Still Long way to go.
There is no need to store any array of numbers or strings.
As the intermediate result can be fit in "long long int". u can perform basic Switch

shuvadip: 2015-04-05 04:50:03

Why is my code showing nzec error of division by zero?

Last edit: 2015-04-05 04:54:07
pankaj joshi: 2015-03-22 19:31:18

focus on last line "it may contain spaces " so code for both the space and non space string .....just try simple approach without using string array etc.

Added by:Fudan University Problem Setters
Time limit:2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: C99 ERL JS-RHINO NODEJS PERL6
Resource:IPSC 2009