n!末尾含0的个数

来源:互联网 发布:安装阿里旺旺mac版 编辑:程序博客网 时间:2024/05/16 06:05

编程之美上的一道题

int containZero(int n){    int count = 0;    /*     方法一: 遍历 1~n 求每个数能拆成多少个5 并相加    while(n > 0){        int m = n;        n--;        while(m % 5 == 0 && m > 0){            m = m/5;            count++;        }    }    return count;    */    // 方法2: 以5的倍数来拆分    while(n){        count += n/5;        n /= 5;    }    return count;}


原创粉丝点击