Sum Mixed Numbers
Python
Medium
4 views
Problem Description
Read n values, each value is either integer or float (contains dot). Sum them and output result with exactly 3 decimals.
Input Format
First line n. Second line n values.
Output Format
One number with 3 decimals.
Sample Test Case
Input:
5
1 2.5 3 0.25 -1.0
Official Solution
import sys,re
p=sys.stdin.read().strip().split()
if not p: sys.exit(0)
n=int(p[0])
a=p[1:1+n]
sm=0.0
for s in a:
if '.' in s:
sm+=float(s)
else:
sm+=int(s)
sys.stdout.write(f'{sm:.3f}')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!