Round to Nearest Multiple
Python
Easy
5 views
Problem Description
Input has two integers n and k are provided (k>0). Round n to nearest multiple of k. If exactly middle, round up.
Input Format
One line: n k.
Output Format
One integer rounded.
Official Solution
import sys
s=sys.stdin.read().strip().split()
if not s: sys.exit(0)
n=int(s[0]); k=int(s[1])
q=n//k
low=q*k
high=(q+1)*k
if n-low < high-n:
ans=low
elif n-low > high-n:
ans=high
else:
ans=high
sys.stdout.write(str(ans))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!