尾部的零

Q: 设计一个算法,计算出n阶乘中尾部零的个数

 

https://www.lintcode.com/problem/trailing-zeros/description 

class Solution:
"""
@param: n: An integer
@return: An integer, denote the number of trailing zeros in n!
"""
def trailingZeros(self, n):
  # write your code here, try to do it without arithmetic operators.
  r=n//5
  s=r
  while r>4:
  r//=5
  s+=r
  return s

  

尾部的0是有5因子的个数决定的,但是有的数可能含有多个5因子,采用类似乘方的方法解决。

老题目新做,有这个感觉漂亮的解法记录一下。

posted on 2020-08-20 17:39  看看完了  阅读(183)  评论(0编辑  收藏  举报

导航