linux用shell判断三角形,[bash]判断三角形类型、计算数学表达式、计算N个整数的指定精度的平均值...

判断三角形为等边三角形、等腰三角形或不等边三角形

#!/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`

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值