2. Trailing Zeros【easy】

Write an algorithm which computes the number of trailing zeros in n factorial.

Have you met this question in a real interview?

Yes
Example

11! = 39916800, so the out should be 2

Challenge

O(log N) time

解法一:

 class Solution {
/*
* param n: As desciption
* return: An integer, denote the number of trailing zeros in n!
*/
public long trailingZeros(long n) {
long count = ;
for(int i = ; Math.pow(,i) <= n; i++) {
count += n / (long)Math.pow(,i);//注意强制类型转换
}
return count;
}
};

求末尾0的个数: 
至于一个数阶乘的末尾有多少个0,0的个数为(其中的“/”是取整除法): 
例子:1000的阶乘末尾0的个数 
1000/5 + 1000/25 + 1000/125 + 1000/625 
= 200 + 40 + 8 + 1 
= 249(个)

原理是: 
假如你把1 × 2 ×3× 4 ×……×N中每一个因数分解质因数,结果就像: 
1 × 2 × 3 × (2 × 2) × 5 × (2 × 3) × 7 × (2 × 2 ×2) ×…… 
10进制数结尾的每一个0都表示有一个因数10存在——任何进制都一样,对于一个M进制的数,让结尾多一个0就等价于乘以M。 
10可以分解为2 × 5——因此只有质数2和5相乘能产生0,别的任何两个质数相乘都不能产生0,而且2,5相乘只产生一个0。 
所以,分解后的整个因数式中有多少对(2, 5),结果中就有多少个0,而分解的结果中,2的个数显然是多于5的,因此,有多少个5,就有多少个(2, 5)对。 
所以,讨论1000的阶乘结尾有几个0的问题,就被转换成了1到1000所有这些数的质因数分解式有多少个5的问题。 
5的个数可以用上面那个式子算出,所以1000的阶乘结尾有249个0。

至于为什么用这个式子,可以见下例: 
求26的阶乘的尾部有多少个0. 
26! = 1 × 2 ×3× 4 × 5 × 6 × 7 × 8 × 9 × 10 × 11 × 12 × 13× 14 × 15 × 16 × 17 × 18 × 19 × 20 × 21 × 22 ×23× 24 × 25 × 26 
内有 26/5 + 26/25 = 6(个)5相乘 
因为25其实可以分解成2个5相乘,而26/5只计算了一个5,因此还要再加26/25。

参考自:http://blog.csdn.net/wutingyehe/article/details/46882181

解法二:

 class Solution {
public:
// param n : description of n
// return: description of return
long long trailingZeros(long long n) {
long long sum = ;
while (n != ) {
sum += n / ;
n /= ;
}
return sum;
}
};

2. Trailing Zeros【easy】的更多相关文章

  1. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  2. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  3. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  4. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  5. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  6. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  7. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

  8. 141. Linked List Cycle【easy】

    141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...

  9. 237. Delete Node in a Linked List【easy】

    237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...

随机推荐

  1. [CF865C]Gotta Go Fast

    题目大意: 一个游戏关卡有$n(n\le50)$个任务,若在$m$秒内按顺序完成所有任务则算作通过当前关卡.每个关卡有三个属性$a_i,b_i,p_i(1\le a_i<b_i\le100,80 ...

  2. python基础-匿名函数、内置函数、正则表达式、模块

    1. 匿名函数 1.1 有名函数 有名函数:定义了一个函数名,函数名指向内存地址:通过函数名进行访问.函数名加括号就可以运行有名函数,例如:func() def func(x, y, z = 1): ...

  3. TQ2440学习笔记——Linux上I2C驱动的两种实现方法(1)

    作者:彭东林 邮箱:pengdonglin137@163.com 内核版本:Linux-3.14 u-boot版本:U-Boot 2015.04 硬件:TQ2440 (NorFlash:2M   Na ...

  4. php漏洞挖掘与代码审计方法

    在甲方公司做代码审计一般还是以白盒为主,漏洞无非这么几类,XSS.sql注入.命令执行.上传漏洞.本地包含.远程包含.权限绕过.信息泄露等. 1.xss + sql注入 其中占大头的自然是XSS与SQ ...

  5. JAMon监控SQL执行时间

    JAMon监控web工程方法的调用性能 http://www.cnblogs.com/zfc2201/p/3786365.html 这往往篇文章主要告诉大家如何监控web方法调用时间,在这个基础这上, ...

  6. unity GPU bound or CPU bound

    unity判断GPU CPUbound android 用unity profiler 里面的cpu时间 xcode有直接的显示

  7. java源码阅读LinkedList

    1类签名与注释 public class LinkedList<E> extends AbstractSequentialList<E> implements List< ...

  8. java压缩 GZIP进行简单压缩,ZIP进行多文件保存

    java压缩  GZIP进行简单压缩,ZIP进行多文件保存 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlhbmdydWkxOTg4/font/5a6 ...

  9. 对Socket CAN的理解(5)——【Socket CAN控制器的初始化过程】

    转载请注明出处:http://blog.csdn.net/Righthek 谢谢! 对于一般的CAN模块,进行初始化时,最关键的是下面两步: 1.  配置CAN的位时序: 2.  配置CAN的消息报文 ...

  10. Java8 新的日期和时间API(笔记)

    LocalDate LocalTime Instant duration以及Period 使用LocalDate和LocalTime //2017-03-20 LocalDate date = Loc ...