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. 【最短路】【最大流】bzoj3931 [CQOI2015]网络吞吐量

    跑出最短路图,然后把结点拆点跑最大流. #include<cstdio> #include<queue> #include<cstring> #include< ...

  2. [Hybrid App]--Android混合开发,Android、Js的交互

    AndroidJs通信 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !imp ...

  3. Problem S: 零起点学算法14——三位数反转

    #include<stdio.h> #include<stdlib.h> int main() { int a,b,c,s; scanf("%d",& ...

  4. Chrome插件在页面上直接绑定JavaScript事件提示Refused to execute inline event handler because it violates the following Co

    Chrome插件问了安全是不提倡在页面上直接写JavaScript的,如果出现了这个提示,其实也没有什么,同样可以运行. 从Chrome Extenstion V2开始,不允许执行任何inline j ...

  5. 【教训】 form表单提交时,action url中参数无效

    今天提交一个表单,内容参考如下: <form action="add.php?a=123&b=456">     <input type="hi ...

  6. 【spring data jpa】jpa中使用count计数方法

    spring data jpa中使用count计数方法很简单 直接在dao层写方法即可 int countByUidAndTenementId(String parentUid, String ten ...

  7. Node.js 调用 restful webservice

    如何构建一个restful web service参考原来的文章 http://www.cnblogs.com/ericnie/p/5212748.html 直接用原来的项目编译好像有问题,此处耗费1 ...

  8. DICOM中几个判断图像方向的tag

    在DICOM标准里,有三个TAG与成像的方向相关. 参考来源:Kitware关于DICOM方向的说明 http://public.kitware.com/IGSTKWIKI/index.php/DIC ...

  9. 腾讯云linux服务器分区方案

    刚刚在腾讯云买了一台服务器,刚买的服务器的数据盘都是需要自己来分区的,下面就记录一下操作. 通过命令fdisk-l查看硬盘信息 可以看到有两块硬盘/dev/vda和/dev/vdb,启动vda是系统盘 ...

  10. javascript快速入门23--XHR—XMLHttpRequest对象

    创建XMLHttpRequest对象 与之前众多DOM操作一样,创建XHR对象也具有兼容性问题:IE6及之前的版本使用ActiveXObject,IE7之后及其它浏览器使用XMLHttpRequest ...