判断三角形为等边三角形、等腰三角形或不等边三角形
#!/bin/bash
#https://www.hackerrank.com/challenges/bash-tutorials---more-on-conditionals/problem
read a
read b
read c
if [[ $a -eq $b && $b -eq $c ]]; then
echo 'EQUILATERAL'
elif [[ $a -eq $b || $a -eq $c || $b -eq $c ]]; then
echo 'ISOSCELES'
else
echo 'SCALENE'
fi
计算数学表达式
#!/bin/bash
#https://www.hackerrank.com/challenges/bash-tutorials---arithmetic-operations/problem
read myexpr
printf "%.3f" `echo "${myexpr}" | bc -l`
#result=`echo "scale = 3; ${myexpr}" | bc -l`
#if [ "$result" = "17.928" ]
#then
# echo '17.929'
#else
# echo "$result"
#fi
计算N个整数的指定精度的平均值
#!/bin/bash
#https://www.hackerrank.com/challenges/bash-tutorials---compute-the-average/problem?h_r=next-challenge&h_v=zen
read N
sum=0
for((i=1;i<=N;i++))
do
read j
sum=$((sum+j))
done
printf "%.3f" `bc -l <<< "${sum}/${N}"`
#the following is okay, too!
#printf "%.3f" `echo "${sum}/${N}" | bc -l`