题目一:
链接:阶乘后零的个数
解题思路:
简单递归:
1! =1 =>0
2 =2*1=2 =>0
3! =3*2*1=6 =>0
4! =4*3*2*1=24 =>0
5! =5*4*3*2*1=120 =>1
....
10! =5*2*9*8*7*6*5*4*3*2=3628800 =>2
20!=5*4*19*18*17*16*5*3*14*13*12*11*5*2*9*8*7*6*5*4*3*2*1
=432902008176640000 =>4
所有由上述可知:
阶乘后零的个数=5的个数,出口也就是n小于5
C++:
class Solution {
public:
int trailingZeroes(int n) {
if(n<5) return 0;
else{
return trailingZeroes(n/5)+n/5;
}
}
};
题目二:
链接:将数字变为0