172.阶乘后的零
![image-20231214120411503](https://img-blog.csdnimg.cn/img_convert/152011aa60bc9ee49b62362f39ec6e37.png)
方法:
![image-20231214120423670](https://img-blog.csdnimg.cn/img_convert/ed5474ab163baa270b45185872f5d11e.png)
class Solution {
public int trailingZeroes(int n) {
int ans = 0;
for(int i = 5;i<=n;i+=5){
for(int x = i; x % 5==0; x/=5){
++ans;
}
}
return ans;
}
}
进一步优化:
![image-20231214120531985](https://img-blog.csdnimg.cn/img_convert/5f7edf80c50d03cc3d1f918b29b46477.png)
class Solution {
public int trailingZeroes(int n) {
int ans = 0;
while (n != 0) {
n /= 5;
ans += n;
}
return ans;
}
}