[bash]判斷三角形型別、計算數學表示式、計算N個整數的指定精度的平均值
阿新 • • 發佈:2018-11-26
判斷三角形為等邊三角形、等腰三角形或不等邊三角形
#!/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`