LeetCode Factorial Trailing Zeroes
2015-08-14 12:24
429 查看
原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/
求factorial后结尾有多少个0,就是求有多少个2和5的配对。
但是2比5多了很多,所以就是求5得个数。但是有的5是叠加起来的比如 25,125是5的幂数,所以就要降幂。
e.g. n = 100, n/5 =20, n/25= 4, n/125=0,所以加起来就有24个0.
AC Java:
求factorial后结尾有多少个0,就是求有多少个2和5的配对。
但是2比5多了很多,所以就是求5得个数。但是有的5是叠加起来的比如 25,125是5的幂数,所以就要降幂。
e.g. n = 100, n/5 =20, n/25= 4, n/125=0,所以加起来就有24个0.
AC Java:
public class Solution { public int trailingZeroes(int n) { //count number of 5 and 2, but 2 is much more than 5, so just count 5 int res = 0; while(n>0){ res += n/5; n /= 5; } return res; } }
相关文章推荐
- LeetCode Factorial Trailing Zeroes
- epoll_create, epoll_ctl和epoll_wait
- 2015 Multi-University Training Contest 2(hdu 5300 - hdu 5309)
- Clock Skew , Clock Uncertainty和 Period
- libvirtError: no connection driver available for qemu:///system 解决办法
- 2015 Multi-University Training Contest 8 hdu 5384 Danganronpa
- tools:context=".MainActivity的作用
- 使用Grails快速开发Web应用程序
- 构建您的第一个Grails 应用程序
- 2015 Multi-University Training Contest 1(hdu 5288 - hdu 5299)
- hdu1021Fibonacci Again找规律
- PS和AI软件区别
- HDU - 3394 Railway(连通分量+环)
- qmail 相关问题
- 智能改变未来TurboMail邮件系统招商新计划
- UVA 10986 Sending email 【dijkstra + 堆优化】
- POJ 1363 Rails
- 如何利用OpenCV自带的haar training程序训练分类器
- 使用OGG的logdump工具检查OGG trail file中存在不存在相关表的记录
- pair的用法