lintcode-easy-Trailing Zeros
2016-03-07 17:43
555 查看
Write an algorithm which computes the number of trailing zeros in n factorial.
11! = 39916800, so the out should be 2
这道题的思路比较诡异,结尾要有0的话,必须要有质因数2和质因数5,而任何一个数的阶乘,质因数2的个数会比质因数5多很多,所以只需要算出有多少个质因数5就可以了
11! = 39916800, so the out should be 2
这道题的思路比较诡异,结尾要有0的话,必须要有质因数2和质因数5,而任何一个数的阶乘,质因数2的个数会比质因数5多很多,所以只需要算出有多少个质因数5就可以了
class Solution { /* * param n: As desciption * return: An integer, denote the number of trailing zeros in n! */ public long trailingZeros(long n) { // write your code here if(n <= 0) return 0; long result = 0; for(long i = 5; n / i > 0; i *= 5){ result += n / i; } return result; } };
相关文章推荐
- 直线mian函数跳转到mainret = main(argc, argv, envp);
- 五子棋AI算法第七篇-Zobrist
- Linux下“read failed after 0 of 4096 at 0: Input/output error”
- URAL 1017 Staircases 楼梯
- VC6.0 error LNK2001: unresolved external symbol _main解决办法
- process launch failed: Security 解决方案
- Rails
- 1106. Lowest Price in Supply Chain (25)
- 利用thumbnailer API来提取图标视频或专辑的thumbnail
- poj 1995 Raising Modulo Numbers(快速幂模板)
- sendmail使用
- [leetcode] 11. Container With Most Water
- 可伸缩性, 可用性和稳定性模式 Scalability, Availability & Stability Patterns
- saiku - 优化多维度查询效率
- 【Xamarin挖墙脚系列:在VMware11中安装Mac10.11 EI Captain后的vmware tools】
- 常用的三种邮件协议
- URAL 1017 Staircases
- Intel 82599 ixgbe & ixgbevf CNA 卡驱动分析02——VF/PF/MailBox
- Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]
- C# Linq 交集、并集、差集、去重http://blog.csdn.net/joyhen/article/details/39578325