RETURNCHANGE - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: abhi_inav
Tester: tabr
Editorialist: iceknight1093

DIFFICULTY:

763

PREREQUISITES:

None

PROBLEM:

Chef purchased an item costing X rupees; and did so using a 100-rupee bill.
Find the amount of change received by Chef if X is rounded to the nearest 10.

EXPLANATION:

Let X' be the rounded value of X. If we are able to find X', then the answer is simply 100 - X'.

According to the rules given, we round to the nearest 10; and round up in case of ambiguity.
That is, if the last digit of X is 0, 1, 2, 3, or 4, X is rounded down; and otherwise, it’s rounded up.

So,

  • Find the last digit of X, say it’s d.
  • If d \leq 4, round X down by subtracting d from it — i.e, X' = X - d
  • Otherwise, d \geq 5 and X needs to be rounded up by adding 10 - d to it — i.e, X' = X + (10 - d).

After X' is found based on the above rules, print 100 - X' as the answer.

TIME COMPLEXITY

\mathcal{O}(1) per test case.

CODE:

Editorialist's code (Python)
for _ in range(int(input())):
    x = int(input())
    if x%10 >= 5: x += 10 - x%10
    else: x -= x%10
    print(100 - x)