原题地址

n!含有多少个因子10,则结尾有多少个0

10=2*5,而2的个数肯定比5多,所以n!含有多少个因子5,则结尾有多少个0

如何计算n!有多少个因子5呢?

比如n=13,则:

n! = 13 * 12 * 11 *  * 9 * 8 * 7 * 6 *  * 4 * 3 * 2 * 1
| |
含有因子5: 10 5

实际上我们就是在找1到n里面能被5整除的数字有多少个。

显然每隔5个数就有一个带因子5的数字出现,所以就是n/5嘛,错!这样还是少算了一些数,比如25=5*5,丫可是包含2个5的,所以还要加上那些每隔25个数、125个数、...出现的数字(5^i)。

代码:

 int trailingZeroes(int n) {
if (n < ) return -;
int res = ;
while (n > )
res += (n /= );
return res;
}

《Cracking the Code》里面也有这道题,这是书上的代码:

 int trailingZeroes(int n) {
if (n < ) return ;
int count = ;
for (int i = ; n / i > ; i *= )
count += n / i;
return count;
}

这个代码是无法通过Leetcode测试的,因为i *= 5会溢出,而前面的代码不会。

Leetcode#172 Fractorial Trailing Zero的更多相关文章

  1. [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  2. LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  3. Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes

    题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...

  4. ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  5. Java for LeetCode 172 Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  6. Java [Leetcode 172]Factorial Trailing Zeroes

    题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...

  7. [LeetCode] 172. Factorial Trailing Zeroes_Easy tag: Math

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  8. Leetcode 172 Factorial Trailing Zeroes

    给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...

  9. leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)

    数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...

随机推荐

  1. ThinkPHP之中的验证码的小示例

    ThinkPHP之中已经封装好了验证码的调用,但是关于手册,缺失了HTML之中以及.实际操作之中的点击ajax就会刷新验证码ajax代码:现在分享一下:看客老爷们注意啦! 放大招啦!!!三分归元气-- ...

  2. C++ 里 构建动态二维数组

    //****动态二维数组 /* int m=3; int **data; int n=2; data=new int*[m]; for(int j=0;j<m;j++) { data[j]=ne ...

  3. spark概论

    一.概述 1.轻:(1)采用语言简洁的scala编写:(2)利用了hadoop和mesos的基础设施   2.快:spark的内存计算.数据本地性和传输优化.调度优化,使其在迭代机器学习,ad-hoc ...

  4. 第十一章 管理类型(In .net4.5) 之 管理对象的生命周期

    1. 概述 本章内容包括 管理非托管资源.使用IDisposable接口 以及 管理析构器和垃圾回收. 2. 主要内容 2.1 理解垃圾回收机制 ① 代码执行的时候,内存中有两个地方存放数据项:堆 和 ...

  5. jquery.validate新的写法(jquery.validate1.13.js)

    <script src="../js/jquery.js"></script> <script src="../js/jquery.vali ...

  6. 16.python中的浅拷贝和深拷贝

    在讲什么是深浅拷贝之前,我们先来看这样一个现象: a = ['scolia', 123, [], ] b = a[:] b[2].append(666) print a print b

  7. m3u8

    audo apt-get install pkg-configsudo apt-get install automake autoconf m4 libtool sudo apt-get instal ...

  8. 九度oj 1541 二叉树

    原题链接:http://ac.jobdu.com/problem.php?pid=1541 简答题如下: #include<algorithm> #include<iostream& ...

  9. android开发系列之socket编程

    上周在项目遇到一个接口需求就是通讯系列必须是socket,所以在这篇博客里面我想谈谈自己在socket编程的时候遇到的一些问题. 其实在android里面实现一个socket通讯是非常简单的,我们只需 ...

  10. Qt的QTabelWidget

    QTableWidget的用法总结  http://blog.csdn.net/zb872676223/article/details/39959061 [Qt]在QTableWidget中添加QCh ...