平均计算Bash中 [英] compute Average in Bash

查看:117
本文介绍了平均计算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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆