递归算法的核心就是对自身的不断调用,但是必须要有递归出口
1、计算阶乘
int factorial(int n){
if(n == 1)
return 1;//这里就是的递归的出口
else
return n * factorial(n - 1);
}
2、汉罗塔问题
void Hanoi (int n, char A, char B, char C){
if (n==1){ //end condition
move(A,B);//‘move’ can be defined to be a print function递归出口
}
else{
Hanoi(n-1,A,C,B);//move sub [n-1] pans from A to B
move(A,C);//move the bottom(max) pan to C
Hanoi(n-1,B,A,C);//move sub [n-1] pans from B to C
//当三个或两个递归在一块的时候,按顺序执行,之后递归
}
}