当前位置: 代码迷 >> 综合 >> Trailing Zeros
  详细解决方案

Trailing Zeros

热度:56   发布时间:2023-09-30 08:08:30.0

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

Example

11! = 39916800, so the out should be 2

Challenge 
Tags 

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;}
};

  相关解决方案