Trailing Zeros

版权声明:本文为博主原创文章,未经博主允许不得转载。如果您有更好的想法,请留言 https://blog.csdn.net/qq_34552886/article/details/79959681

Write an algorithm which computes the number of trailing zeros in n factorial.

Example

11! = 39916800, so the out should be 2

Related Problems 

一开始没想明白,后来随手列了一些相乘尾数能等于0的数,2*5,4*5,5*6,5*8。发现都是与5相关。所以其实就是求5的数量

class Solution {
public:
    /*
     * @param n: A long integer
     * @return: An integer, denote the number of trailing zeros in n!
     */
    long long trailingZeros(long long n) {
        // write your code here, try to do it without arithmetic operators.
      long long sum=0;
      while(n){
          sum+=n/5;
          n=n/5;
      }
      return sum;
    }
};

阅读更多
换一批

没有更多推荐了,返回首页