Factorial Trailing Zeroes 172
题目描述:
给出一个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相关推荐
- 172. Factorial Trailing Zeroes
/**172. Factorial Trailing Zeroes *2016-6-4 by Mingyang* 首先别忘了什么是factorial,就是阶乘.那么很容易想到需要统计* (2,5)对的 ...
- LeetCode 172. Factorial Trailing Zeroes
LeetCode 172. Factorial Trailing Zeroes 问题来源LeetCode 172. Factorial Trailing Zeroes 问题描述 Given an in ...
- 【LeetCode】172 - Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 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 ...
- Leetcode 172 Factorial Trailing Zeroes
1.题目要求 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- leetcode python3 简单题172. Factorial Trailing Zeroes
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十二题 (1)题目 英文: Given an integer n, retu ...
- 172 Factorial Trailing Zeroes(阶乘后的零)————附带详细思路和代码
文章目录 0 效果 1 题目 2 思路 3 代码 0 效果 1 题目 Given an integer n, return the number of trailing zeroes in n!. N ...
- LeetCode(172) Factorial Trailing Zeroes
题目如下: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- [LeetCode] Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
最新文章
- 程序员毕业两年,如何在帝都购房上车?
- Boost:显示如何将累加器持久化到文件中
- 2011年第二届蓝桥杯决赛 —— C语言本科 —— 第一题
- 前端学习(3212):解决类中的this问题
- mysql不使用swapp的原因_SolidWorks不能使用的原因
- Python学习笔记:爬取网页图片
- ApplicationId 与 PackageName
- AR人工智能模型训练计算机,ar人工智能模型训练一般采用什么计算机
- 感悟:决定一个系统走势是多因素构成,而非单一因素决定
- Android 应用和系统优化V1.2
- ICCV2021 多目标跟踪(MOT)汇总
- ERP系统和CRM系统
- 传智java完整教学视频
- python的十句名言_让人恍然大悟的十句名言(双语版)
- freemaker导出Excel文件用WPS能打开,而用office打不开
- python类初始化返回实例_Python基础——类、实例及初始化
- ble l2cap 工作过程_从车联网安全到BLE安全(二)
- PHP 文档标签添加 间隔符“多空格”处理
- 三井化学将增加TAFMER™高性能弹性体产能
- 拉格朗日插值、分段线性插值、三次样条插值
热门文章
- 贷款为什么要查看征信?
- Booting ARM Linux SMP on MPCore
- Oracle数据库事务回滚和提交,数据库 事务提交和回滚
- 阿联酋esma认证_阿联酋无人驾驶汽车预计2021年上路
- springboot jwt token前后端分离_为什么要 前后端分离 ?
- selinux= 为 disabled_「丁香园」五年没考过执医,就不要出来「为祸人间」了
- mysql 层_mysql三层体系
- vue created 调用方法_vue 基础-生命周期 lifecycle 的执行顺序和作用
- 内存泄漏java例子_一次线上Java应用内存泄漏分析实例
- city如何使用 mega unity_制作思路和CityEngine的简单使用