[阶乘]leetcode172:阶乘后的零(easy)

mac2022-06-30  26

题目: 题解:

1)本题是求阶乘后的0,而0通过5 * 2=10求得,然而我们不必管2的个数,因为偶数就会产生2,所以我们只需要计算5的个数就好了。2)不过需要注意类似31!中的25=5 * 5有两个5,所以我们不仅需要统计当前小于n的数中有多少个5,还需要统计n的商中是否存在5。

代码如下:

class Solution { public: int trailingZeroes(int n) { int count=0; while(n>=5) { count+=n/5; //当前小于n的5的倍数的个数 n/=5; //还需要判断类似5*5这种情况存在多个5的,所以还要判断n的商中是否存在5的倍数 } return count; } };
最新回复(0)