原题地址

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. php中iconv函数使用方法

    最近在做一个程序,需要用到iconv函数把抓取来过的utf-8编码的页面转成gb2312, 发现只有用iconv函数把抓取过来的数据一转码数据就会无缘无故的少一些. iconv函数库能够完成各种字符集 ...

  2. ubuntu打开 txt 文件乱码

    ubuntu12.04 gedit 打开 windows 分区中的 txt 文件乱码,是因为 ubuntu 和 windows 两个系统的编码不同.解决办法:终端里依次输入以下2 条命令即可: 代码: ...

  3. Android SDK中国在线更新镜像服务器 解决GOOGLE更新无法下载 更新失败的问题

    Android Tools Android SDK在线更新镜像服务器 中国科学院开源协会镜像站地址: IPV4/IPV6: http://mirrors.opencas.cn 端口:80 IPV4/I ...

  4. Jquery + echarts 使用

    常规用法,就不细说了,按照官网一步步来. 本文主要解决问题(已参考网上其他文章): 1.把echarts给扩展到JQuery上,做到更方便调用. 2.多图共存 3.常见的X轴格式化,钻取时传业务实体I ...

  5. Moses manual 中Basline System 2.3.4节用IRSTLM创建语言模型的命令有误

    手册里写到: ~/irstlm/bin/compile-lm \ --text yes \ news-commentary-v8.fr-en.lm.en.gz \ news-commentary-v8 ...

  6. EMVTag系列13《脱机PIN》

    DGI8010用于个人化借记贷记交易中使用的脱机PIN.数据强制要求加密.制卡数据传输过程中,此DGI采用DEK加密保护. 数据分组标识 '8010'的数据内容       要求            ...

  7. Oracle Database Gateway 安装

    在[Oracle HS (Heterogeneous Services)深入解析 及协同Gateway工作流程]一文中主要主要介绍了HS的工作原理,及其如何协同Gateway一起工作.那么了解Gate ...

  8. android开发分辨率问题解决方案

    dpi是什么呢?dpi是“dot per inch”的缩写,每英寸像素数.四种密度分类: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extr ...

  9. Android 文档之viewAnimator

    一.结构 public class ViewAnimator extends FrameLayout java.lang.Object android.view.View android.view.V ...

  10. Extjs-工具条和菜单 Ext.menu和Ext.Toolbar

    转载自:http://blog.csdn.net/itlwc/article/details/7878002 1.创建一个简单工具条效果图 <script type="text/jav ...