S2Q2 · Ceil Marks to Nearest Tens if Close¶
⚡ Quick Reference
Type: Full I/O problem
Core idea: if the units digit of the marks is 8 or 9, round up to the next ten; print only updated records.
n = int(input())
for _ in range(n):
roll, marks = input().split()
marks = int(marks)
if marks % 10 in (8, 9):
updated = (marks // 10 + 1) * 10
print(f"{roll},{updated}")
Key rules:
- Units digit 8 or 9 → round up to next ten
- All other marks → unchanged, not printed
- Output format: roll_number,updated_marks (comma-separated, no space)
Problem Statement¶
Problem (I/O type)
Read n student records (roll number and marks). If marks end in 8 or 9, ceil to the next ten. Print only the records that were updated.
Example:
4
R01 48
R02 69
R03 70
R04 37
R01,50
R02,70
R03 (marks=70) and R04 (marks=37) are unchanged - not printed.
Tracing the example¶
| Roll | Marks | Units digit | Updated? | New marks | Print? |
|---|---|---|---|---|---|
| R01 | 48 | 8 ✅ | ✅ | (4+1)×10 = 50 | ✅ |
| R02 | 69 | 9 ✅ | ✅ | (6+1)×10 = 70 | ✅ |
| R03 | 70 | 0 ❌ | ❌ | - | ❌ |
| R04 | 37 | 7 ❌ | ❌ | - | ❌ |
Solution approaches¶
import math
n = int(input())
for _ in range(n):
roll, marks = input().split()
marks = int(marks)
if marks % 10 in (8, 9):
updated = math.ceil(marks / 10) * 10
print(f"{roll},{updated}")
math.ceil(marks / 10) * 10 rounds up to the next multiple of 10. Equivalent to (marks // 10 + 1) * 10 when units digit is 8 or 9.
Key takeaways¶
marks % 10 in (8, 9) - clean membership test
Using in (8, 9) is cleaner than == 8 or == 9. The in operator checks membership in any iterable - a tuple is idiomatic for small sets of values.
(marks // 10 + 1) * 10 rounds up to next ten
Floor divide by 10 gives the current tens digit, adding 1 moves to the next, multiplying by 10 scales back up. For marks=48: (4+1)×10 = 50. For marks=69: (6+1)×10 = 70.
Only print updated records
The print is inside the if block - unchanged records are silently skipped. No need to store all records; process and print in one pass.