Factorial Trailing Zeroes
Factorial Trailing Zeroes
Total Accepted: 44612 Total Submissions: 144778 Difficulty: Easy
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
n!的结果中0的个数。
0是由5与偶数相乘产生的,所以我们只需计算0-n中有多个“5”,注意25包含两个5 , 125包含3个5。
所以: cnt = n/5+n/25+n/125+...
class Solution { public: int trailingZeroes(int n) { int res = 0; while(n){ res += n/5; n /= 5; } return res; } };
写者:zengzy
出处: http://www.cnblogs.com/zengzy
标题有【转】字样的文章从别的地方转过来的,否则为个人学习笔记