Factorial Trailing Zeroes
2015-09-24 21:10
387 查看
【题目描述】
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
【思路】
看n的阶乘里有多少个0,只要看1~n中有多少个2和5就可以了,又因为2肯定比5多,所以只要看5有多少个就可以了。另外,还要注意像25里包含了2个5,125里包含了3个5.。。
【代码】
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
【思路】
看n的阶乘里有多少个0,只要看1~n中有多少个2和5就可以了,又因为2肯定比5多,所以只要看5有多少个就可以了。另外,还要注意像25里包含了2个5,125里包含了3个5.。。
【代码】
class Solution { public: int trailingZeroes(int n) { int ans=0; while(n){ ans+=n/5; n=n/5; } return ans; } };
相关文章推荐
- HDU 3394 Railway(点双连通分量与桥)
- AIDL相关的解释远程service和client处理
- rotate图片旋转 http://blog.csdn.net/lonelyroamer/article/details/8252533
- MethodInvokingJobDetailFactoryBean的并发问题
- blog is waiting for you
- Could not obtain transaction-synchronized Session for current thread
- Linux sync命令的作用 http://blog.csdn.net/holandstone/article/details/7356865
- HDU 2476 String painter(记忆化搜索, DP)
- copypng emitted errors but did not return a nonzero exit code to indicate failure
- 超级块与索引节点 http://blog.csdn.net/ilsas/article/details/5627348
- TIME_WAIT状态的必要性
- POJ 1273 Drainage Ditches(最大流模板)
- MailMessage From 及Sender的區別
- mycat AIO改写成NIO 性能从80000tps增加到100000tps
- 磁盘RAID
- BIO/NIO/AIO的文章
- MFC中改变对话框背景的几个消息函数OnEraseBkgnd、 OnPaint、 OnCtlColor的调用顺序
- Unity 3D 使用Relief Terrain Pack(RTP) 问题
- Windows AIK+Imagex+DISM+U盘PE+U盘安装win7+PE
- rails4 will_paginate ajax 局部刷新