递归,尾递归

 

  def main(args: Array[String]): Unit = {
    val res1 = fact(4);
    val res2 = fact2(4,1)
    println(res1)
    println(res2)
  }
  //递归
  def factorial(n:Int):Int={
    if(n<=0){
      1
    }else{
    n*factorial(n-1)
    }
  }
//尾递归
  def tailFactorial(n:Int,acc:Int):Int={
    if(n<=1) acc
    else tailFactorial(n-1,acc*n)
  }

 

posted @ 2017-09-14 13:24  牵牛花  阅读(115)  评论(0编辑  收藏  举报