Leetcode#172 Fractorial Trailing Zero
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的更多相关文章
- [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 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 ...
- ✡ 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 ...
- 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 ...
- Java [Leetcode 172]Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- [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 ...
- Leetcode 172 Factorial Trailing Zeroes
给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...
- leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)
数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...
随机推荐
- js生成有缩进的表格
项目中用到用了两天时间想到的,记录下来,如有更好的方法,留言给我,谢谢! js做如下表格: json [{"id":302,"serviceId":15,&qu ...
- Java输出1~1000之间所有可以被3整除又可以被5整除的数
主要在于判断是否能被整除,思路是用取余运算符%,取余结果为0就表示能被整除. 代码如下: public class NumDemo { public static void main(String a ...
- 【转】Spark 体系结构
原文地址:http://jerryshao.me/architecture/2013/03/29/spark-overview/ 援引@JerryLead的系统架构图作为Spark整体结构的一个 bi ...
- PHP-You don’t have permissions to access xxx on this server!
问题如下图: 如果你是想要查看目录下的每一个文件,那么你需要修改一下httpd-conf配置文件,也就是apache的配置文件,以phpStudy2013为例,如下图打开: 然后找到如下部分,添加 ...
- PHP引用文件
require: 可能多次执行的代码用require效率要稍高 require_once: 唯一区别是 PHP 会检查该文件是否已经被包含过,如果是则不会再次包含 include: 语句包含并运行指定 ...
- 关于fseek和文件"ab+"打开方式的问题
这是在写一个文件的的时候发生的一个错误,代码如下 #include<stdio.h> #include <errno.h> #include <string.h> ...
- 11.python中的元组
在学习什么是元组之前,我们先来看看如何创建一个元组对象: a = ('abc',123) b = tuple(('def',456)) print a print b
- JSTL实现分页
JSTL(JSP Standard Tag Library ,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的.JSTL只能运行在支持JSP1. ...
- ORA-01207: file is more recent than control file -
OS: [root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 DB: Oracle Database 11g E ...
- ios中怎么样调节占位文字与字体大小在同一高度
在设置好字体以后,在占位文字中设置leading这个字体属性,用leading来乘以一个比例(CGFloat)来调节位置.