shell递归求阶乘

2019-01-24 15:20:02

shell递归求阶乘

  1. #!/bin/bash
  2. function factorial()
  3. if [ ${1} -eq 1 ] ; then
  4. echo 1
  5. else
  6. local temp=$[${1} - 1]
  7. local result=`factorial ${temp}`
  8. echo $[${result} * ${1}]
  9. fi
  10. read -p "输入一个值:" value
  11. result=`factorial ${value}`
  12. echo "${value}的阶乘等于:${result}"

0
1
0

添加评论

正在回复:
取消
2
0
1
0