通过例子来实现:
public class factorial {
public static void main(String[] args) {
System.out.println(f(6));
}
public static long f(int n) {
if(n==1) {
return 1;
}else {
return n*f(n-1);
}
}
}
迭代:简而言之,就是A不停的调用B。通过另一个例子实现:
public class iteration {
public static void main(String[] args) {
System.out.println(f(20));
}
public static long f(int index) {
if(index==1||index==2) {
return 1l;
}
long f1=1l;
long f2=1l;
long f=0;
for(int i=0;i
f=f1+f2;
f1=f2;
f2=f;
}
return f;
}
}