shell 递归求阶乘

2019-06-11 11:15:28 浏览数 (1)

代码语言:javascript复制
#!/bin/bash

fact()
{
  local num=$1
  local fac
   if ((num==1))
   then
      fac=1
   else
       ((dec=num-1))
       fact $dec
       fac=$?
       fac=`expr $num * $fac`
    fi
   return $fac
}

fact 5

echo $?                    

0 人点赞