列表

详情


NP52. 累加数与平均值

描述

牛牛有一个列表,记录了他和同事们的年龄,你能用for循环遍历链表的每一个元素,将其累加求得他们年龄的总和与平均数吗?

输入描述

一行输入多个整数,以空格间隔。

输出描述

输出年龄总和与平均数,平均数保留1位小数,两个数字以空格间隔。

示例1

输入:

22 23 24

输出:

69 23.0

原站题解

Python 3 解法, 执行用时: 27ms, 内存消耗: 4440KB, 提交时间: 2022-08-05

list1=list(map(int,(input().split(" "))))
a=0
for i in list1:
    a+=i
print("%d %.1f" %(a,a/len(list1)))

Python 3 解法, 执行用时: 27ms, 内存消耗: 4456KB, 提交时间: 2022-08-02

n = list(map(int, input().split()))
sum = 0
for i in n:
    sum += i

print(sum,'%.1f' %(sum/len(n)))

Python 3 解法, 执行用时: 28ms, 内存消耗: 4396KB, 提交时间: 2022-08-06

num = list(map(int,input().split()))
sum = 0
for i in num:
    sum+=i
print(sum,'{:.1f}'.format(sum/len(num)))

Python 3 解法, 执行用时: 28ms, 内存消耗: 4404KB, 提交时间: 2022-08-04

lt = [int(i) for i in input().split()]
s =sum(lt)
ave = s/len(lt)
print(s,'%.1f' %ave)

Python 3 解法, 执行用时: 28ms, 内存消耗: 4404KB, 提交时间: 2022-07-29

age = input().split()
sum = 0
for i in age:
    sum=sum+int(i)
print('{} {:.1f}'.format(sum,sum/len(age)))

上一题