递归初探

public class TestDataStructure {

public static void main(String[] args) {
int n = 4;
int total = factorial(n);
System.out.println(total);//24
}

//阶乘递归算法
public static int factorial(int n){
if(n<=1){
return 1;
}else{
int subSolution = factorial(n-1);
int solution = subSolution*n;
return solution;
}
}

}

posted @ 2019-12-13 15:36  表演给自己看的认真  阅读(114)  评论(0编辑  收藏  举报