Factorial Trailing Zeroes
2015-08-14 13:44
323 查看
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
分解因子, 因子中出现 一对 (2,5)时, 最后结果会增加一个0.
25可以分解出5*5,此时(2,5),(4,5)都不会得到0。125会得到三个0.
所以要求的和是 sum(N/5^1, N/5^2, N/5^3...)。
Note: Your solution should be in logarithmic time complexity.
分解因子, 因子中出现 一对 (2,5)时, 最后结果会增加一个0.
25可以分解出5*5,此时(2,5),(4,5)都不会得到0。125会得到三个0.
所以要求的和是 sum(N/5^1, N/5^2, N/5^3...)。
public int trailingZeroes(int n) { if (n < 0) return -1; int count = 0; for(long i=5;n/i>=1;i*=5) count+=n/i; return count; }
相关文章推荐
- F - Stone-----(2015 summer training #11)
- HDU4300-Clairewd’s message-KMP
- E - Theme Section-----(2015 summer training #11)
- D - Cut the Cake----(2015 summer training #11)
- error: linker command failed with exit code 1 (use -v to see invocation)
- J - A Bit Fun-----(2015 summer training #9)
- SGU183 Painting the balls(DP+优化)
- D - Minimum palindrome----(2015 summer training #9)
- C - We Love MOE Girls-----(2015 summer training #9)
- B - Median-----(2015 summer training #8(Qualifying))
- Could not connect to '10.7.100.182' (port 22): Connection failed
- HIT 2060 Fibonacci Problem Again
- LeetCode Factorial Trailing Zeroes
- 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的作用