zoukankan      html  css  js  c++  java
  • python面试题

    author:刘大神

    求n阶乘末尾连续0的个数,如5!=120,末尾0个数为1.

    class Solution2:
      #方案1 使用递归 当数值足够大的时候递归会报错
    def trailingZeroes(self, n): if isinstance(n, int) and n >= 0: zeroes = lambda k: k // 5 + zeroes(k // 5) if k // 5 else 0 return n // 5 + zeroes(n // 5) if n // 5 else 0 else: return '输入不合法'   #方案2 使用循环 循环则不会发生这种问题 def trailingZeroes2(self,n): if isinstance(n, int) and n >= 0: i = 0 while n > 0: i += n // 5 n = n // 5 return i else: return '输入不合法' n = 10**2000 import time now = time.time() print(Solution2().trailingZeroes2(n), time.time() - now) now = time.time() print(Solution2().trailingZeroes(n), time.time() - now)
  • 相关阅读:
    17-DBSCAN密度聚类
    16-K-means聚类
    15-TF-IDF
    14-支持向量机SVM
    13-感知机原理概述
    12-随机森林
    11-集成学习原理概述
    10-决策树
    9-朴素贝叶斯
    栈和队列(python)
  • 原文地址:https://www.cnblogs.com/hao66/p/9294503.html
Copyright © 2011-2022 走看看