Decimal Drift Simulator

Decimal Drift Simulator

Hard Python Data Types 12 views
Explanation Complexity

Problem Statement

Read x and n. Repeat n times: x = round(x,2) + 0.1 (decimal rounding half up). Output final x with exactly 2 decimals.

Input Format

• First line: Two values x n

• x is a decimal number

• n is an integer

Output Format

• Print final value of x after repeating operation n times

• Output with exactly 2 decimal places

• Use decimal rounding half up

Example

1.005 2
1.30

Constraints

• 0 ≤ n ≤ 10^5

• -10^9 ≤ x ≤ 10^9

Concept Explanation

Start with x = 1.005

Operation repeated 2 times:

Step 1:
round(1.005, 2) using half up → 1.01
x = 1.01 + 0.1 = 1.11

Step 2:
round(1.11, 2) → 1.11
x = 1.11 + 0.1 = 1.21

Finally round to 2 decimals → 1.30

Step-by-Step Explanation

1.Read x as string and n as integer.

2.Use Decimal from decimal module for accurate half-up rounding.

3.Convert x to Decimal.

4.Repeat n times:

• Round x to 2 decimal places using ROUND_HALF_UP.

• Add Decimal("0.1") to x.

5.After loop, round final x to 2 decimal places again.

6.Print formatted result with exactly 2 decimals.

Concept Explanation

Start with x = 1.005

Operation repeated 2 times:

Step 1:
round(1.005, 2) using half up → 1.01
x = 1.01 + 0.1 = 1.11

Step 2:
round(1.11, 2) → 1.11
x = 1.11 + 0.1 = 1.21

Finally round to 2 decimals → 1.30

Step-by-Step Explanation

1.Read x as string and n as integer.

2.Use Decimal from decimal module for accurate half-up rounding.

3.Convert x to Decimal.

4.Repeat n times:

• Round x to 2 decimal places using ROUND_HALF_UP.

• Add Decimal("0.1") to x.

5.After loop, round final x to 2 decimal places again.

6.Print formatted result with exactly 2 decimals.

Input / Output Format

Input Format
• First line: Two values x n

• x is a decimal number

• n is an integer
Output Format
• Print final value of x after repeating operation n times

• Output with exactly 2 decimal places

• Use decimal rounding half up
Constraints
• 0 ≤ n ≤ 10^5

• -10^9 ≤ x ≤ 10^9

Examples

Input:
1.005 2
Output:
1.30

Example Solution (Public)

Python
import sysnfrom decimal import Decimal,ROUND_HALF_UPnp=sys.stdin.read().strip().split()nif not p: sys.exit(0)nx=Decimal(p[0])nn=int(p[1])nstep=Decimal('0.1')nfor _ in range(n):n  x=x.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)+stepnx=x.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)nsys.stdout.write(f'{x:.2f}')

Official Solution Code

import sysnfrom decimal import Decimal,ROUND_HALF_UPnp=sys.stdin.read().strip().split()nif not p: sys.exit(0)nx=Decimal(p[0])nn=int(p[1])nstep=Decimal('0.1')nfor _ in range(n):n  x=x.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)+stepnx=x.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)nsys.stdout.write(f'{x:.2f}')
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.