递归,尾递归
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) }
学好计算机,走遍天下都不怕