一次累计 n 个整数,同时输出总和和五位小数均值。
OJ: noi_openjudge
题目 ID: ch0105-04
难度:入门
标签:循环数学python
日期: 2026-07-30 23:01
题意
读入
思路
总和只需累计一次。平均值直接由 total / count 得到,最后在同一条 f-string 中输出总和和 .5f 格式化结果。
代码
Python代码
python
count = int(input())
total = sum(int(input()) for _ in range(count))
print(f"{total} {total / count:.5f}")C++代码
cpp
#include <cstdio>
int main(){
int n;
int sum = 0;
int i,t;
scanf("%d",&n);
for (i=1;i<=n;i++){
scanf("%d",&t);
sum +=t;
}
printf("%d ",sum);
printf("%0.5lf",sum*1.0/n);
return 0;
}复杂度
时间复杂度为
总结
需要总和和均值时,不必保存全部输入;总和与数量已足够。