题目描述:

给出一个integer n,计算n!结尾0的个数


题目分析:

考虑暴力,计算n!统计最后面0的个数。先不说数字溢出,其次n是一个integer ,O(n)复杂度超时

我们接着考虑,产生0的情况

只有包含因子5的数乘以一个偶数会在结尾产生0(5*2,15*2,75*2),因为偶数的个数大于因子包含5的数的个数,那么我们只要考虑包含因子5的数;

但是是不是包含因子5的数一定只会在结尾产生一个0呢,对于25*4提供两个0,这是因为25,包含两个因子5(5*5);同样125会提供3个0;

这样只要考虑1-n之间有多少包含因子5的数,有多少个包含因子25的数,有道少个包含因子125的数。。。。以此累加


代码:

1 int trailingZeroes(int n) {
2         int mod=5;
3         int cnt=0;
4         while(n/mod){
5             cnt+=n/mod;
6             mod*=5;
7         }
8         return cnt;
9     }

转载于:https://www.cnblogs.com/li-xingtao/p/4215246.html

Factorial Trailing Zeroes 172相关推荐

  1. 172. Factorial Trailing Zeroes

    /**172. Factorial Trailing Zeroes *2016-6-4 by Mingyang* 首先别忘了什么是factorial,就是阶乘.那么很容易想到需要统计* (2,5)对的 ...

  2. LeetCode 172. Factorial Trailing Zeroes

    LeetCode 172. Factorial Trailing Zeroes 问题来源LeetCode 172. Factorial Trailing Zeroes 问题描述 Given an in ...

  3. 【LeetCode】172 - Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  4. Leet Code OJ 172. Factorial Trailing Zeroes [Difficulty: Easy]

    题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...

  5. Leetcode 172 Factorial Trailing Zeroes

    1.题目要求 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...

  6. leetcode python3 简单题172. Factorial Trailing Zeroes

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十二题 (1)题目 英文: Given an integer n, retu ...

  7. 172 Factorial Trailing Zeroes(阶乘后的零)————附带详细思路和代码

    文章目录 0 效果 1 题目 2 思路 3 代码 0 效果 1 题目 Given an integer n, return the number of trailing zeroes in n!. N ...

  8. LeetCode(172) Factorial Trailing Zeroes

    题目如下: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...

  9. [LeetCode] Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

最新文章

  1. 程序员毕业两年,如何在帝都购房上车?
  2. Boost:显示如何将累加器持久化到文件中
  3. 2011年第二届蓝桥杯决赛 —— C语言本科 —— 第一题
  4. 前端学习(3212):解决类中的this问题
  5. mysql不使用swapp的原因_SolidWorks不能使用的原因
  6. Python学习笔记:爬取网页图片
  7. ApplicationId 与 PackageName
  8. AR人工智能模型训练计算机,ar人工智能模型训练一般采用什么计算机
  9. 感悟:决定一个系统走势是多因素构成,而非单一因素决定
  10. Android 应用和系统优化V1.2
  11. ICCV2021 多目标跟踪(MOT)汇总
  12. ERP系统和CRM系统
  13. 传智java完整教学视频
  14. python的十句名言_让人恍然大悟的十句名言(双语版)
  15. freemaker导出Excel文件用WPS能打开,而用office打不开
  16. python类初始化返回实例_Python基础——类、实例及初始化
  17. ble l2cap 工作过程_从车联网安全到BLE安全(二)
  18. PHP 文档标签添加 间隔符“多空格”处理
  19. 三井化学将增加TAFMER™高性能弹性体产能
  20. 拉格朗日插值、分段线性插值、三次样条插值

热门文章

  1. 贷款为什么要查看征信?
  2. Booting ARM Linux SMP on MPCore
  3. Oracle数据库事务回滚和提交,数据库 事务提交和回滚
  4. 阿联酋esma认证_阿联酋无人驾驶汽车预计2021年上路
  5. springboot jwt token前后端分离_为什么要 前后端分离 ?
  6. selinux= 为 disabled_「丁香园」五年没考过执医,就不要出来「为祸人间」了
  7. mysql 层_mysql三层体系
  8. vue created 调用方法_vue 基础-生命周期 lifecycle 的执行顺序和作用
  9. 内存泄漏java例子_一次线上Java应用内存泄漏分析实例
  10. city如何使用 mega unity_制作思路和CityEngine的简单使用