LeetCode-127
Links:https://leetcode.com/problems/factorial-trailing-zeroes/
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
题目大意:求n!的末尾有多少个0…
思路:计算n!的质因数中5的个数。
因为:10 = 2*5(即只有一对2和5的时候才进位,又2比5个数多…)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Solution { public: int trailingZeroes(int n) { if(n<=4)return 0; else{ int c = 0; while(n) { n/=5; c+=n; } return c; } } }; |
【LeetCode】172. Factorial Trailing Zeroes