题目链接:点击这里
求
后缀零的个数,也就是
的个数,也就
的个数,由于
比
的次数要少,因此统计
的个数即可。
class Solution {
public:
int trailingZeroes(int n) {
int cnt = 0;
//for(int i = 1; i <= n; i++) {
for(int i = 5; i <= n; i += 5) {
int x = i;
while(x%5==0) {
cnt++;
x /= 5;
}
}
return cnt;
}
};
优化:
class Solution {
public:
int trailingZeroes(int n) {
int cnt = 0;
while(n>0) {
cnt += n/5;
n /= 5;
}
return cnt;
}
};