1! = 1
2! = 1*2
3! = 1*2*3
4! = 1*2*3*4
5! = 1*2*3*4*5
public static void main(String[] args) {
System.out.println(f(3));
}
public static int f(int n) {
if(n==1) {
return 1;
}else {
return n*f(n-1);
}
}
}
理解:
当n=1 return 1
当n不为1,
n=2 return 2f(2-1)
n=3 return 3f(3-1)=3f(2) —>f(2)=>return 2f(2-1)=2*f(1)