平均计算Bash中 [英] compute Average in Bash
问题描述
问题陈述
由于N个整数,计算它们的平均值,正确到小数点后三位。
Given N integers, compute their average, correct to three decimal places.
输入格式
第一行包含一个整数N.
其次是N个整数,每一个新行。
Input Format The first line contains an integer N. This is followed by N integers, each on a new line.
输出格式
显示N个整数的平均值,四舍五入到小数点后三位。
Output Format Display the average of the N integers, rounded off to three decimal places.
输入约束
1 <= N <= 500
-10000 <= x <= 10000 (x refers to elements of the list of integers for which the average is to be computed)
采样输入
4
1
2
9
8
示例输出
5.000
的解释
在第一行中的4表示有四个整数,其平均值是要计算。平均=(1 + 2 + 9 + 8)/ 4 = 20/4 = 5.000(正确到小数点后三位)请包括零,即使它们是多余的(如0.000,而不是0)。
The '4' in the first line indicates that there are four integers whose average is to be computed. The average = (1 + 2 + 9 + 8)/4 = 20/4 = 5.000 (correct to three decimal places) Please include the zeroes even if they are redundant (eg. 0.000 instead of 0).
推荐答案
您可以使用此awk命令:
You can use this awk command:
awk 'NR==1{n=$1;next} {s+=$1} END{printf "%.3f\n", s/n}' file
5.000
这篇关于平均计算Bash中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!