<分区>
我正在服用 a challenge on hackerrank . 目标是:
read (int) N; then read N integers and print their avg to three decimal places.
代码如下:
#!/bin/bash
#file name:rdlp.sh
read N
s=0
i=1
while (($i<=$N))
do
read a
s=$((s+a))
i=$((i+1))
done
s=$s/$N
echo "scale=3;$s"|bc -l
fi
当我为某些输入运行代码时:
3 #(value of N)
4 #(N = 3 integers)
4
3
那么输出是3.666,但应该是3.667。
所以问题是无论如何要让它正确(正确四舍五入),或者它只是那样工作吗?
(当上面的代码为 Testcase2 of the challenge at hackerrank 运行时问题出现了)