LeetCode Factorial Trailing Zeroes
2015-09-12 20:24
441 查看
思路:
求阶乘的末尾0的个数。
n的阶乘可以写成: n!=2x∗3y∗5z...n! = 2^x*3^y*5^z...
即所有质因子的乘积,只有一对2和5两个质因子相乘会贡献一个末尾0,又因为x一定大于z,所以只需计算有多少个质因子5即可。
求阶乘的末尾0的个数。
n的阶乘可以写成: n!=2x∗3y∗5z...n! = 2^x*3^y*5^z...
即所有质因子的乘积,只有一对2和5两个质因子相乘会贡献一个末尾0,又因为x一定大于z,所以只需计算有多少个质因子5即可。
class Solution { public: int trailingZeroes(int n) { int ans = 0; while(n) { ans += n/5; n /= 5; } return ans; } };
相关文章推荐
- STL 中iterator_traits
- main函数和启动例程(Startup Routine)
- 深入解读Quartz的原理(job-jobDetail-Trigger-scheduler)
- rails学习笔记:维护商品信息任务A
- POJ 3411 Paid Roads
- feof 编辑 http://baike.baidu.com/link?url=9jDAPGbHghkBT0Wd_vYusfglfk8GnCUda921gIyMwheS6P-Gy-xP8Ep
- 使用行为树(Behavior Tree)实现游戏AI
- URL tailing slash
- hdu5029 Relief grain 点权树链剖分,线段树
- 【LeetCode】172. Factorial Trailing Zeroes
- Drainage Ditches---hdu1532(最大流, 模板)
- bundle update rails 报错处理
- State模式及与Strategy/Commmand/Chain of Resposibility区别
- kamailio服务器内容补充
- storcli中raid级别之间的转换
- HUD1022 Train Problem I
- Contains Duplicate II 解答
- hdoj 1787 GCD Again【欧拉函数】
- pthread_barrier_wait()源码解析
- Linux 文件系统(三)---dup和fork函数执行后的文件情况 http://blog.csdn.net/shanshanpt/article/details/39049579