『质因数分解』题解 | #阶乘末尾0的数量#

阶乘末尾0的数量

http://www.nowcoder.com/practice/aa03dff18376454c9d2e359163bf44b8

01.超时解法

  • 但是方法是对的
class Solution {
public:
    /**
     * the number of 0
     * @param n long长整型 the number
     * @return long长整型
     */
    long long thenumberof0(long long n) {
        // write code here
        long long res=0;
        for(long long loop=5; loop<=n; loop+=5 )
        {
            long long temp=loop;
            while( 0==temp%5 )
            {
                ++res;
                temp/=5;
            }
        }

        return res;
    }
};
  • 测试如下
    运行超时:您的程序未能在规定时间内运行结束,请检查是否循环有错或算法复杂度过大。
    6/11 组用例通过
    运行时间
    2001ms
    占用内存
    428KB

02.数学公式

class Solution {
public:
    /**
     * the number of 0
     * @param n long长整型 the number
     * @return long长整型
     */
    long long thenumberof0(long long n) {
        // write code here
        long long res=0;
        while( n )
        {
            res+=( n/5 );
            n/=5;
        }

        return res;

    }
};
全部评论

相关推荐

人力小鱼姐:实习经历没有什么含金量,咖啡店员迎宾这种就别写了,其他两段包装一下 想找人力相关的话,总结一下个人优势,结合校园经历里有相关性的部分,加一段自我评价
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-04 15:36
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务