递归函数就是直接或间接调用自身的函数,也就是自身调用自己
例子
1.递归求和1+2+3+.....+n
public static Integer recursionSum(Integer n){undefined
if(n>0){undefined
return n+recursionSum(n-1);
}else{undefined
return 0;
}
}
2.递归阶乘n! = n * (n-1) * (n-2) * ...* 1(n>0)
public static Integer recursionMulity(Integer n){undefined
if(n==1){undefined
return 1;
}
return n*recursionMulity(n-1);
}
3.河内塔问题
4.判定一系列字符串中是否有相同的内容
public static boolean fun(int n,String[] a){undefined
boolean b = false;
if(n == a.length){undefined
b = true;
}else{undefined
for(int i = n; i < a.length-1; i++){undefined
System.out.println(n+" "+(i+1));
if(a[n].equals(a[i+1])){undefined
return false;
}
}
n++;
fun(n,a);
}
return b;
}