172. Factorial Trailing Zeroes

来源:互联网 发布:java中什么叫泛型 编辑:程序博客网 时间:2024/05/22 01:26
/*Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.n!中包含多少尾随的010=2*5   n!中包含的0的个数就是因子25的个数,在n!中 2的个数多于5的个数,所以只要统计有多少个5即可。eg: 125=5*5*535n/5 表示有多少个数含有5这个因子 ,但像125这个数含有多个因子,需要重复迭代,即先求出n/5(含有5的数的个数) 后,n/=5 即除去每个数中的5(数中如果含有因子5,因子5的个数减一) 重复上述过程 直到n=0*/class Solution {public:    int trailingZeroes(int n) {        int res=0;        for(int i=n/5;i>0;i=n/5){            n/=5;            res+=n;        }        return res;    }};
原创粉丝点击